[英]Thread entered in to deadlock condition when using anonymous thread and trying to print odd, even numbers by two thread
我试图通过两个线程重复使用等待和通知打印奇数、偶数。
我可以通过实现 Runnable 接口来实现这一点
public class OddEven implements Runnable {
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == Integer.parseInt(Thread.currentThread().getName())) {
synchronized (this) {
notifyAll();
System.out.println((Thread.currentThread().getName().equals("1") ? "odd : " : "even: ") + i);
try {
if (i != 100)
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
OddEven obj = new OddEven();
Thread t1 = new Thread(obj, "1");
Thread t2 = new Thread(obj, "0");
t1.start();
t2.start();
}
}
O/P is:
Odd : 1
Even: 2
Odd : 3
Even: 4
Odd : 5
.
.
.
.
Odd : 97
Even: 98
Odd : 99
Even: 100
然后我通过扩展线程 class 尝试了同样的事情
public class Test {
public static void main(String[] args) {
Test t = new Test();
new OddThraed(t).start();
new EvenThraed(t).start();
}
}
class OddThraed extends Thread {
Test t;
public OddThraed(Test t) {
this.t = t;
}
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
synchronized (t) {
t.notifyAll();
System.out.println("Odd : " + i);
try {
if (i != 100)
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class EvenThraed extends Thread {
Test t;
public EvenThraed(Test t) {
this.t = t;
}
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
synchronized (t) {
t.notifyAll();
System.out.println("Even: " + i);
try {
if (i != 100)
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
O/P 符合预期
然后我认为有太多的类,并试图通过使用匿名线程来实现这一点
public class TestOddEvenAnonymousThread {
public static void main(String[] args) {
(new Thread("Odd") {
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
synchronized (this) {
notifyAll();
System.out.println("Odd : " + i);
try {
if (i != 100)
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}).start();
(new Thread("Even") {
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
synchronized (this) {
notifyAll();
System.out.println("Even: " + i);
try {
if (i != 100)
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}).start();
}
}
But this time O/P is
Odd : 1
Even: 2
并且有死锁有人可以解释为什么最后一个没有显示适当的o / p,我只是想用匿名线程而不是通过扩展单独的线程类来实现这一点(我在上面的第二种方法中做了)
class TestOddEvenAnonymousThread {
public static void main(String[] args) {
TestOddEvenAnonymousThread t = new TestOddEvenAnonymousThread();
Thread oddT = (new Thread("Odd") {
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
synchronized (t) {
t.notifyAll();
System.out.println("Odd : " + i);
try {
if (i != 100)
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
Thread eventT = (new Thread("Even") {
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
synchronized (t) {
t.notifyAll();
System.out.println("Even: " + i);
try {
if (i != 100)
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
oddT.start();
eventT.start();
}
}
当您在旧线程和事件线程中不知不觉中引用此内容时,您将同步块应用于不同的并调用notifyAll
并wait
这些相应的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.