前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学多线程-3

自学多线程-3

作者头像
py3study
发布2020-01-09 19:51:50
3430
发布2020-01-09 19:51:50
举报
文章被收录于专栏:python3python3

中断线程的运行:

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

代码示例如下:

代码语言:javascript
复制
class Kirl implements Runnable  {      public void printText()      {          System.out.println("Thread1 start sleep");          try         {              Thread.sleep(5000);          }          catch(Exception e)          {              System.out.println("Thread1 block");              return;          }          System.out.println("Thread1 quit");      }      public void run()      {          printText();      }  }  public class TestThread {      /**       * @param args       * @throws Exception        */     public static void main(String[] args) throws Exception {          // TODO Auto-generated method stub          Kirl k1 = new Kirl();          Thread t1 = new Thread(k1,"Kirl");          System.out.println("Kirl start");          t1.start();          System.out.println("Main sleep");          try {              Thread.sleep(3000);          } catch (Exception e) {              // TODO: handle exception          }          System.out.println("Main block start");          t1.interrupt();          System.out.println("Main quit");      }  } 

运行结果如下:

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

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

查看线程的中断状态:

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

示例代码如下:

代码语言:javascript
复制
public class TestThread {      /**       * @param args       * @throws Exception        */     public static void main(String[] args) throws Exception {          // TODO Auto-generated method stub          Thread t = Thread.currentThread();          System.out.println("Time1:" + t.isInterrupted());          t.interrupt();          System.out.println("Time2:" + t.isInterrupted());          System.out.println("Time3:" + t.isInterrupted());          try         {              Thread.sleep(2000);              System.out.println("Interrupted failed!");          }catch(Exception e)          {              System.out.println("Interrupted success!");          }          System.out.println("Time4:" + t.isInterrupted());      }  } 

运行结果如下:

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

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

多线程的同步问题:

代码示例如下:

代码语言:javascript
复制
class Kirl implements Runnable  {      private int ticket = 7;      public synchronized void sell()      {          while(ticket > 0)          {              try {                  Thread.sleep(100);              } catch (InterruptedException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();              }              System.out.println(Thread.currentThread().getName() + "->" + ticket--);          }      }      public void run()      {          this.sell();      }  }  public class TestThread {       /**       * @param args       * @throws Exception        */     public static void main(String[] args) throws Exception {          // TODO Auto-generated method stub          Kirl k = new Kirl();          Thread t1 = new Thread(k,"Thread1");          Thread t2 = new Thread(k,"Thread2");          Thread t3 = new Thread(k,"Thread3");          t1.start();          t2.start();          t3.start();      }  } 

运行结果如下:

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

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

代码语言:javascript
复制
class Kirl implements Runnable  {      private int ticket = 7;      public void sell()      {          while(ticket > 0)          {              synchronized (this) {                  if(this.ticket > 0){                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                  }                  System.out.println(Thread.currentThread().getName() + "->" + ticket--);                  }              }          }      }      public void run()      {          this.sell();      }  }  public class TestThread {      /**       * @param args       * @throws Exception        */     public static void main(String[] args) throws Exception {          // TODO Auto-generated method stub          Kirl k = new Kirl();          Thread t1 = new Thread(k,"Thread1");          Thread t2 = new Thread(k,"Thread2");          Thread t3 = new Thread(k,"Thread3");          t1.start();          t2.start();          t3.start();      }  } 

运行结果如下:

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档