小编Ela*_*lad的帖子

使用wait()时的同步方法

我运行了以下代码:

class Counter extends Thread {

 static int i=0;
//method where the thread execution will start
public void run(){
    //logic to execute in a thread

    while (true) {
        increment();
    }
}

public synchronized void increment()  {
    try {
        System.out.println(this.getName() + " " +  i++);
        wait(1000);
        notify();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
//let’s see how to start the threads
public static void main(String[] args){
    Counter c1 = new Counter();
    Counter c2 = new …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized wait

5
推荐指数
2
解决办法
4814
查看次数

标签 统计

java ×1

multithreading ×1

synchronized ×1

wait ×1