您好,登錄后才能下訂單哦!
中斷線程的運(yùn)行:
當(dāng)一個(gè)線程運(yùn)行時(shí),另一個(gè)線程可以調(diào)用對(duì)應(yīng)的Thread對(duì)象的interrupt()方法來(lái)中斷它。
代碼示例如下:
- 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");
- }
- }
運(yùn)行結(jié)果如下:
Kirl start
Main sleep
Thread1 start sleep
Main block start
Main quit
Thread1 block
由以上結(jié)果可知,當(dāng)Thread1線程執(zhí)行過(guò)程中,Main線程發(fā)出中斷Thread1線程的命令,則Thread1線程被中斷,拋出異常。
查看線程的中斷狀態(tài):
可以在Thread對(duì)象上調(diào)用isInterrupted()方法來(lái)檢查任何線程的中斷狀態(tài)。
示例代碼如下:
- 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());
- }
- }
運(yùn)行結(jié)果如下:
Time1:false
Time2:true
Time3:true
Interrupted success!
Time4:false
由以上結(jié)果可知,線程如果中斷之后再休眠,則會(huì)清除中斷標(biāo)志。
多線程的同步問(wèn)題:
代碼示例如下:
- 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();
- }
- }
運(yùn)行結(jié)果如下:
Thread1->7
Thread1->6
Thread1->5
Thread1->4
Thread1->3
Thread1->2
Thread1->1
由以上結(jié)果可知,雖然實(shí)現(xiàn)了多線程共享資源的問(wèn)題,但只有一個(gè)線程在執(zhí)行,故并不是真正的實(shí)現(xiàn)了多線程的同步功能。即只有一個(gè)代售點(diǎn)在售票。
- 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();
- }
- }
運(yùn)行結(jié)果如下:
Thread2->7
Thread2->6
Thread3->5
Thread3->4
Thread3->3
Thread1->2
Thread1->1
由結(jié)果分析可知,實(shí)現(xiàn)了多線程的同步功能,多個(gè)代售點(diǎn)功能賣票。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。