iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >自学多线程-3
  • 714
分享到

自学多线程-3

多线程 2023-01-31 02:01:39 714人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

中断线程的运行: 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它。 代码示例如下: class Kirl implements Runnable  {      public void pri

中断线程的运行:

当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它。

代码示例如下:

  1. class Kirl implements Runnable  
  2. {  
  3.     public void printText()  
  4.     {  
  5.         System.out.println("Thread1 start sleep");  
  6.         try 
  7.         {  
  8.             Thread.sleep(5000);  
  9.         }  
  10.         catch(Exception e)  
  11.         {  
  12.             System.out.println("Thread1 block");  
  13.             return;  
  14.         }  
  15.         System.out.println("Thread1 quit");  
  16.     }  
  17.     public void run()  
  18.     {  
  19.         printText();  
  20.     }  
  21. }  
  22. public class TestThread {  
  23.      
  24.     public static void main(String[] args) throws Exception {  
  25.         // TODO Auto-generated method stub  
  26.         Kirl k1 = new Kirl();  
  27.         Thread t1 = new Thread(k1,"Kirl");  
  28.         System.out.println("Kirl start");  
  29.         t1.start();  
  30.         System.out.println("Main sleep");  
  31.         try {  
  32.             Thread.sleep(3000);  
  33.         } catch (Exception e) {  
  34.             // TODO: handle exception  
  35.         }  
  36.         System.out.println("Main block start");  
  37.         t1.interrupt();  
  38.         System.out.println("Main quit");  
  39.     }  

运行结果如下:

Kirl start
Main sleep
Thread1 start sleep
Main block start
Main quit
Thread1 block

由以上结果可知,当Thread1线程执行过程中,Main线程发出中断Thread1线程的命令,则Thread1线程被中断,抛出异常。

查看线程的中断状态:

可以在Thread对象上调用isInterrupted()方法来检查任何线程的中断状态。

示例代码如下:

  1. public class TestThread {  
  2.      
  3.     public static void main(String[] args) throws Exception {  
  4.         // TODO Auto-generated method stub  
  5.         Thread t = Thread.currentThread();  
  6.         System.out.println("Time1:" + t.isInterrupted());  
  7.         t.interrupt();  
  8.         System.out.println("Time2:" + t.isInterrupted());  
  9.         System.out.println("Time3:" + t.isInterrupted());  
  10.         try 
  11.         {  
  12.             Thread.sleep(2000);  
  13.             System.out.println("Interrupted failed!");  
  14.         }catch(Exception e)  
  15.         {  
  16.             System.out.println("Interrupted success!");  
  17.         }  
  18.         System.out.println("Time4:" + t.isInterrupted());  
  19.     }  

运行结果如下:

Time1:false
Time2:true
Time3:true
Interrupted success!
Time4:false

由以上结果可知,线程如果中断之后再休眠,则会清除中断标志。

多线程的同步问题:

代码示例如下:

  1. class Kirl implements Runnable  
  2. {  
  3.     private int ticket = 7;  
  4.     public synchronized void sell()  
  5.     {  
  6.         while(ticket > 0)  
  7.         {  
  8.             try {  
  9.                 Thread.sleep(100);  
  10.             } catch (InterruptedException e) {  
  11.                 // TODO Auto-generated catch block  
  12.                 e.printStackTrace();  
  13.             }  
  14.             System.out.println(Thread.currentThread().getName() + "->" + ticket--);  
  15.         }  
  16.     }  
  17.     public void run()  
  18.     {  
  19.         this.sell();  
  20.     }  
  21. }  
  22. public class TestThread {  
  23.  
  24.      
  25.     public static void main(String[] args) throws Exception {  
  26.         // TODO Auto-generated method stub  
  27.         Kirl k = new Kirl();  
  28.         Thread t1 = new Thread(k,"Thread1");  
  29.         Thread t2 = new Thread(k,"Thread2");  
  30.         Thread t3 = new Thread(k,"Thread3");  
  31.         t1.start();  
  32.         t2.start();  
  33.         t3.start();  
  34.     }  

运行结果如下:

Thread1->7
Thread1->6
Thread1->5
Thread1->4
Thread1->3
Thread1->2
Thread1->1

由以上结果可知,虽然实现了多线程共享资源的问题,但只有一个线程在执行,故并不是真正的实现了多线程的同步功能。即只有一个代售点在售票。

  1. class Kirl implements Runnable  
  2. {  
  3.     private int ticket = 7;  
  4.     public void sell()  
  5.     {  
  6.         while(ticket > 0)  
  7.         {  
  8.             synchronized (this) {  
  9.                 if(this.ticket > 0){  
  10.                 try {  
  11.                     Thread.sleep(100);  
  12.                 } catch (InterruptedException e) {  
  13.                     // TODO Auto-generated catch block  
  14.                     e.printStackTrace();  
  15.                 }  
  16.                 System.out.println(Thread.currentThread().getName() + "->" + ticket--);  
  17.                 }  
  18.             }  
  19.         }  
  20.     }  
  21.     public void run()  
  22.     {  
  23.         this.sell();  
  24.     }  
  25. }  
  26. public class TestThread {  
  27.      
  28.     public static void main(String[] args) throws Exception {  
  29.         // TODO Auto-generated method stub  
  30.         Kirl k = new Kirl();  
  31.         Thread t1 = new Thread(k,"Thread1");  
  32.         Thread t2 = new Thread(k,"Thread2");  
  33.         Thread t3 = new Thread(k,"Thread3");  
  34.         t1.start();  
  35.         t2.start();  
  36.         t3.start();  
  37.     }  

运行结果如下:

Thread2->7
Thread2->6
Thread3->5
Thread3->4
Thread3->3
Thread1->2
Thread1->1

由结果分析可知,实现了多线程的同步功能,多个代售点功能卖票。

--结束END--

本文标题: 自学多线程-3

本文链接: https://www.lsjlt.com/news/186113.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 自学多线程-3
    中断线程的运行: 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它。 代码示例如下: class Kirl implements Runnable  {      public void pri...
    99+
    2023-01-31
    多线程
  • Java 多线程学习总结3
    在上一篇中,我们当然希望a++,b++执行完之后,show方法再来show.我们需要的是“原子”动作,一次性地把a++,b++不间断地执行。在java中是利用“互斥”的方法,互斥谁呢?互斥的是相同对象的加锁代码。如果我们把第一篇的SomeB...
    99+
    2023-01-31
    多线程 Java
  • python多线程————3、多线程间通
    1、共享变量 #通过共享变量 import time import threading url_list = [] def get_detail_html(): global url_list while True: ...
    99+
    2023-01-31
    多线程 python
  • 多线程编程(3):线程池ThreadPo
    在面向对象编程中,经常会面对创建对象和销毁对象的情况,如果不正确处理的话,在短时间内创建大量对象然后执行简单处理之后又要销毁这些刚刚建立的对象,这是一个非常消耗性能的低效行为,所以很多面向对象语言中在内部使用对象池来处理这种情况,以提高性能...
    99+
    2023-01-31
    线程 多线程 ThreadPo
  • 学习java多线程
    目录介绍为什么需要多线程线程状态转换线程使用方式继承 Thread 类实现 Runnable 接口实现 Callable 接口同步代码---Runnable接口方式同步方法--Run...
    99+
    2024-04-02
  • python多线程学习
    python多线程学习:python中的线程使用的两种方式:函数或者用类来包装线程对象。1、函数式:调用thread模块中start_new_thread()函数来产生新线程。#!/usr/bin/env python #coding:ut...
    99+
    2023-01-31
    多线程 python
  • Python多线程学习 setDae
    (2)setDaemon方法: # -*- coding: utf-8 -*- import threading import time class myThread(threading.Thread):     def __init...
    99+
    2023-01-31
    多线程 Python setDae
  • Python2.7自学笔记3——流程控制
    一、if语句>>> x = int(raw_input("Please enter an integer: "))Please enter an integer: 42>>> if x < 0:.....
    99+
    2023-01-31
    流程 笔记
  • python多线程之自定义线程类
    '''自定义线程类''' from threading import Thread import time #创建一个类,并继承Python的Thread类,且重写run()方法实现具体的执行顺序由自己来定义 class MyThread...
    99+
    2023-01-31
    自定义 线程 多线程
  • Python多线程入门学习
    Python 中使用线程有两种方式:函数或者用类来包装线程对象。 函数式: 调用 thread 模块中的start_new_thread()函数来产生新线程。 语法如下: thr...
    99+
    2024-04-02
  • 深入学习C#多线程
    目录一、基本概念1、进程2、线程二、多线程2.1System.Threading.Thread类2.2 线程的常用属性2.2.1线程的标识符2.2.2线程的优先级别2.2....
    99+
    2024-04-02
  • 多线程学习初步(转)
    import java.io.*;//多线程编程public class MultiThread {public static void main(String args[]){System.out.println("我是主线程!");//...
    99+
    2023-06-03
  • 多线程的学习上篇
    座右铭: 天行健,君子以自强不息;地势坤,君子以厚德载物. 引入进程这个概念的目的 引入进程这个概念,最主要的目的,是为了解决“并发编程"这样的问题. 这是因为CPU进入了多核心的时代 要想进一步提...
    99+
    2023-09-21
    学习 java 服务器
  • Python学习记录-多进程和多线程
    [TOC] 1. 进程和线程 进程 狭义定义:进程是正在运行的程序的实例(an instance of a computer program that is being executed)。广义定义:进程是一个具有一定独立功能的程序关于某...
    99+
    2023-01-31
    多线程 进程 Python
  • Java多线程学习笔记
    目录多任务、多线程程序、进程、线程学着看jdk文档线程的创建1.继承Thread类2.实现Runable接口理解并发的场景龟兔赛跑场景实现callable接口理解函数式接口理解线程的...
    99+
    2024-04-02
  • Java多线程(3)---锁策略、CAS和JUC
    目录 前言 一.锁策略 1.1乐观锁和悲观锁 ⭐ 两者的概念 ⭐实现方法 1.2读写锁  ⭐概念 ⭐实现方法 1.3重量级锁和轻量级锁 1.4自旋锁和挂起等待锁 ⭐概念 ⭐代码实现 1.5公平锁和非公平锁 1.6可重入锁和不可重入锁 二.C...
    99+
    2023-08-31
    开发语言 多线程 java-ee
  • 带你快速搞定java多线程(3)
    目录一、锁的概念二、synchronized 的使用方式三、synchronized 的实现原理列小结四、线程池是什么五、为什么要用线程池?六、看下类图,从整体上理解下七、线程池的创...
    99+
    2024-04-02
  • python多线程怎么自定义线程类
    这篇文章主要介绍python多线程怎么自定义线程类,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!python的数据类型有哪些python的数据类型:1. 数字类型,包括int(整型)、long(长整型)和float(...
    99+
    2023-06-14
  • Java多线程 自定义线程池详情
    主要介绍: 1.任务队列 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列) 3.init( min ) 4.active 5.max ...
    99+
    2024-04-02
  • SQL 自学笔记3(W3School)
    自学W3School http://www.w3school.com.cn/sql/index.asp 主键和外键 主键 PRIMARY KEY 约束唯一标识数据库表中的每条记录。 主键必须包含唯一的值。 主键列不能包含 NULL 值。 每...
    99+
    2023-01-31
    笔记 SQL W3School
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作