gpt4 book ai didi

java - Java中使用notify时,thread和runnable有什么区别吗?

转载 作者:行者123 更新时间:2023-12-01 06:24:03 25 4
gpt4 key购买 nike

当我尝试在 Java 中对 notify() 进行一些测试时,我发现了一些困惑。

这里我有两个线程b1和b2,它们引用了另一个线程a。在b1和b2中,它们将调用wait()。而在线程a中,它会调用notify()。据我所知,它会调用b1或b2重新开始。当我使用 Runnable 意识到这一点时,确实如此。但是当我使用 Thread 意识到这一点时,b1 和 b2 都再次启动。谁能解释一下这一点吗?

这是代码。

使用Runnable实现:

线程A:

public class threada implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name+" started");
synchronized(this){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name + "end");
notify();
}
}
}

线程B:

public class threadb implements Runnable{

private threada ta;
public threadb(threada a){
ta = a;
}

@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name + " started");
synchronized(ta){
try {
ta.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name + "end");
}
}

}

主要:

public class threadmain {
public static void main(String...args){
threada a = new threada();

threadb b1 = new threadb(a);
threadb b2 = new threadb(a);

new Thread(b1, "b1").start();
new Thread(b2, "b2").start();

new Thread(a, "a").start();
}
}

结果:

b1 started
a started
b2 started
aend
b1end

使用Thread实现:

线程A:

public class ThreadA extends Thread{

@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+" started");
synchronized(this){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name + "end");
notify();
}
}
}

线程B:

public class ThreadB extends Thread{

private ThreadA ta;
public ThreadB(ThreadA a){
ta = a;
}
@Override
public void run() {
// TODO Auto-generated method stub
// super.run();
String name = Thread.currentThread().getName();
System.out.println(name + " started");
synchronized(ta){
try {
ta.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name + "end");
}
}
}

主要:

public class ThreadMain {
public static void main(String... args) throws InterruptedException {
ThreadA a = new ThreadA();

ThreadB b1 = new ThreadB(a);
ThreadB b2 = new ThreadB(a);
b1.setName("b1");
b2.setName("b2");

b1.start();
b2.start();

Thread.sleep(10);

a.setName("a");
a.start();
}
}

结果:

b1 started
b2 started
a started
aend
b1end
b2end

最佳答案

有趣的是,您的问题的解决方案记录在 Thread.join()

As a thread terminates the this.notifyAll method is invoked

因此,您的一个线程被 notify() 调用唤醒,另一个线程被线程 A 结束并因此向其自身发送 notifyAll() 唤醒。因此,每次线程死亡时,等待它的所有内容都会被唤醒。

对于 Runnable 版本,这种情况不会发生,因为等待链接到 Runnable 对象而不是线程。

关于java - Java中使用notify时,thread和runnable有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com