自学多线程-3
中断线程的运行:
当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它。
代码示例如下:
- 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 {
-
- 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()方法来检查任何线程的中断状态。
示例代码如下:
- public class TestThread {
-
- 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
由以上结果可知,线程如果中断之后再休眠,则会清除中断标志。
多线程的同步问题:
代码示例如下:
- 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 {
-
-
- 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
由以上结果可知,虽然实现了多线程共享资源的问题,但只有一个线程在执行,故并不是真正的实现了多线程的同步功能。即只有一个代售点在售票。
- 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 {
-
- 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
由结果分析可知,实现了多线程的同步功能,多个代售点功能卖票。
相关文章