温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

自学多线程-3

发布时间:2020-06-19 12:19:58 来源:网络 阅读:797 作者:KIRL 栏目:开发技术

中断线程的运行:

当一个线程运行时,另一个线程可以调用对应的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.      * @param args  
  25.      * @throws Exception   
  26.      */ 
  27.     public static void main(String[] args) throws Exception {  
  28.         // TODO Auto-generated method stub  
  29.         Kirl k1 = new Kirl();  
  30.         Thread t1 = new Thread(k1,"Kirl");  
  31.         System.out.println("Kirl start");  
  32.         t1.start();  
  33.         System.out.println("Main sleep");  
  34.         try {  
  35.             Thread.sleep(3000);  
  36.         } catch (Exception e) {  
  37.             // TODO: handle exception  
  38.         }  
  39.         System.out.println("Main block start");  
  40.         t1.interrupt();  
  41.         System.out.println("Main quit");  
  42.     }  

运行结果如下:

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.      * @param args  
  4.      * @throws Exception   
  5.      */ 
  6.     public static void main(String[] args) throws Exception {  
  7.         // TODO Auto-generated method stub  
  8.         Thread t = Thread.currentThread();  
  9.         System.out.println("Time1:" + t.isInterrupted());  
  10.         t.interrupt();  
  11.         System.out.println("Time2:" + t.isInterrupted());  
  12.         System.out.println("Time3:" + t.isInterrupted());  
  13.         try 
  14.         {  
  15.             Thread.sleep(2000);  
  16.             System.out.println("Interrupted failed!");  
  17.         }catch(Exception e)  
  18.         {  
  19.             System.out.println("Interrupted success!");  
  20.         }  
  21.         System.out.println("Time4:" + t.isInterrupted());  
  22.     }  

运行结果如下:

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.      * @param args  
  26.      * @throws Exception   
  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.     }  

运行结果如下:

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.      * @param args  
  29.      * @throws Exception   
  30.      */ 
  31.     public static void main(String[] args) throws Exception {  
  32.         // TODO Auto-generated method stub  
  33.         Kirl k = new Kirl();  
  34.         Thread t1 = new Thread(k,"Thread1");  
  35.         Thread t2 = new Thread(k,"Thread2");  
  36.         Thread t3 = new Thread(k,"Thread3");  
  37.         t1.start();  
  38.         t2.start();  
  39.         t3.start();  
  40.     }  

运行结果如下:

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI