繁体   English   中英

所有值仅通过一个线程打印

[英]all values are printing through one thread only

正在等待再次启动(处于等待状态)的Thread-0即使在通过另一个Thread命中通知之后也没有启动。有人帮助我使用下面的代码。

尝试使用两个线程thread-0,thread-1打印数字但是一旦线程进入等待状态,即使在另一个线程调用notify之后它也没有启动

package com.chan.newFeature;
class ValuePrinter  implements  Runnable
{

    private volatile  static int maxValue=1;
    private int reminder;
    public ValuePrinter(int reminder) {
        this.reminder = reminder;
    }
    public ValuePrinter() {

    }

    @Override
    public void run() {

        while(maxValue<20){
            synchronized (this){
             //   System.out.println("maxValue "+maxValue);
            /*if (maxValue%2==0)
                printEven();
            else
                printOdd();*/
            if (maxValue%2==reminder){
                try {
                   System.out.println("Inside Synchronized Context Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);
                   this.wait();
                    System.out.println("Inside Synchronized Context after wait Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
           //     System.out.println("Thread.currentThread().getName()"+Thread.currentThread().getName());
                System.out.println("Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue+":"+reminder);
                this.notify();
                System.out.println("Thread.currentThread().getName() :");
                maxValue=maxValue+1;
            }
        }
    }


}







public class OddEvenExample {


    public static void main(String[] args) {
        ValuePrinter valuePrinter=new ValuePrinter(2);
        ValuePrinter valuePrinter2=new ValuePrinter(1);
        Thread t1=new Thread(valuePrinter);
        Thread t2=new Thread(valuePrinter2);
        t1.start();
        t2.start();
    }
}

Thread.currentThread().getName() :Thread-0:1:2
Inside Synchronized Context Thread.currentThread().getName() :Thread-1:1
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:2:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:3:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:4:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:5:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:6:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:7:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:8:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:9:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:10:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:11:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:12:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:13:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:14:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:15:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:16:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:17:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:18:2
Thread.currentThread().getName() :
Thread.currentThread().getName() :Thread-0:19:2
Thread.currentThread().getName() :

我编辑了我的答案:这个,“通过notifyAll()替换notify()”不是正确的答案,但问题在于: 为什么我不能使用notifyAll()来唤醒等待的线程? 您正在创建两个独立的对象,而不是在同一个锁中。 简单来说,您的通知仅涉及该特定对象线程。

在这里,我得到了你想要的答案:

static Object lock = new Object();

@Override
public void run() {

     while(maxValue<50){
            synchronized (lock){
             //   System.out.println("maxValue "+maxValue);
            /*if (maxValue%2==0)
                printEven();
            else
                printOdd();*/
            if (maxValue%2==reminder){
                try {
                   System.out.println("Inside Synchronized Context Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);
                   lock.wait();
                   System.out.println("Inside Synchronized Context after wait Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Thread.currentThread().getName() :"+Thread.currentThread().getName() +":"+maxValue+":"+reminder);
            lock.notifyAll();
            maxValue=maxValue+1;
            }
        }

}

为公共锁添加一个静态对象属性,因此锁定此对象而不是自身线程。 如果这不起作用,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM