您好,登錄后才能下訂單哦!
中斷線程的運行:
當一個線程運行時,另一個線程可以調用對應的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 {
- /**
- * @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()方法來檢查任何線程的中斷狀態。
示例代碼如下:
- 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
由以上結果可知,線程如果中斷之后再休眠,則會清除中斷標志。
多線程的同步問題:
代碼示例如下:
- 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
由以上結果可知,雖然實現了多線程共享資源的問題,但只有一個線程在執行,故并不是真正的實現了多線程的同步功能。即只有一個代售點在售票。
- 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
由結果分析可知,實現了多線程的同步功能,多個代售點功能賣票。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。