首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java并发性-中断DeadLook线程

问java并发性-中断DeadLook线程
EN

Stack Overflow用户
提问于 2012-08-05 11:38:56
回答 1查看 883关注 0票数 1

我正在尝试学习java并发的基础知识,我从oracle java教程开始,直到http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html,我只是在代码示例中添加了一些行,但当我运行代码时,main方法永远不会返回。我知道alphonseThread和gastonThread都被以太锁住了,但是为什么不能从主线程中断它们?

代码语言:javascript
复制
public class DeadLock {

     static class Freind {

          private final String name;
          public Freind(String name_) {
               this.name = name_;
          }

          public String getName() {
               return this.name;
          }

          public synchronized void bow(Freind bower) throws InterruptedException {
                System.out.format("%s : %s " + "has bowed to me!%n",
                      this.name, bower.getName());
                Thread.sleep(2000);
                bower.bowBack(this);

         }

         public synchronized void bowBack(Freind bower) {
               System.out.format("%s : %s " + "has bowed back to me!%n",
                this.name, bower.getName());
         }
     }

    @SuppressWarnings("SleepWhileInLoop")
    public static void main(String[] args) throws InterruptedException {
        // wait for 4 seconds before send a interuption signal
        int patient = 4000;
        int waitingUnit = 1000;
        long start;
        final Freind alphonse = new Freind("Alphonse");
        final Freind gaston = new Freind("Gaston");
        // the first thread 
        Thread alphonseThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    alphonse.bow(gaston);
                } catch (InterruptedException ex) {
                    System.out.println("Alphnso have been killed");
                }
             }
        });
        // the second thread
        Thread gastonThread = new Thread(new Runnable() {
            @Override
            public void run() {
                 try {
                     gaston.bow(alphonse);
                 } catch (InterruptedException ex) {
                     System.out.println("Gaston have been killed ");
                 }
             }
         });
         // start the tow threads                
         start = System.currentTimeMillis();
         alphonseThread.start();
         gastonThread.start();
         while (alphonseThread.isAlive() || gastonThread.isAlive()) {
             if (((System.currentTimeMillis() - start) < patient)
                     && (alphonseThread.isAlive() || gastonThread.isAlive())) {
                 System.out.println(System.currentTimeMillis() - start);
                 System.out.println("still waiting !!");
                 Thread.sleep(waitingUnit);
              } else {
                 if (alphonseThread.isAlive()) {
                     alphonseThread.interrupt();
                     alphonseThread.join();
                 }
                 if (gastonThread.isAlive()) {
                     gastonThread.interrupt();
                     gastonThread.join();
                }                     
             }
         }
         System.out.println("finnaly I kill all of them");
     }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-05 12:49:54

但是为什么不能从主线程中断它们呢?

您可以从主线程(或任何地方)中断。只需在您想要中断的线程上调用Thread.interrupt()。

问题是,interrupt不会解除试图获取Java原语锁/互斥锁的线程的阻塞。事实上,我不认为有任何东西可以安全地解锁在原语锁上的线程。

如果您希望您的应用程序能够从涉及对象锁的死锁中恢复,那么您可能应该使用Lock对象,而Lock.lockInterruptibly()方法...它将在线程被中断时解除阻塞。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11813766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档