使用线程打印奇偶数
使用线程进行奇偶数打印。创建一个线程类,两个线程实例。一个将打印奇数,另一个将打印偶数。
我做了以下编码。但到了死锁状态。有人可以解释一下这可能是什么原因吗?
public class NumberPrinter implements Runnable{
private String type;
private static boolean oddTurn=true;
public NumberPrinter(String type){
this.type=type;
}
public void run() {
int i=type.equals("odd")?1:2;
while(i<10){
if(type.equals("odd"))
printOdd(i);
if(type.equals("even"))
printEven(i);
i=i+2;
}
}
private synchronized void printOdd(int i){
while(!oddTurn){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(type + i);
oddTurn=false;
notifyAll();
}
private synchronized void printEven(int i){
while(oddTurn){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(type + i);
oddTurn=true;
notifyAll();
}
public static void main(String[] s){
Thread odd=new Thread(new NumberPrinter("odd"));
Thread even=new Thread(new NumberPrinter("even"));
odd.start();
even.start();
}
}
输出: 奇数1 Even2
然后陷入僵局!!!!!!
感谢您的帮助。
Odd even number printing using thread.Create one thread class, two instance of the thread. One will print the odd number and the other will print the even number.
I did the following coding. But it comes to dead lock state. Can some one please explain what might be the reason for that?
public class NumberPrinter implements Runnable{
private String type;
private static boolean oddTurn=true;
public NumberPrinter(String type){
this.type=type;
}
public void run() {
int i=type.equals("odd")?1:2;
while(i<10){
if(type.equals("odd"))
printOdd(i);
if(type.equals("even"))
printEven(i);
i=i+2;
}
}
private synchronized void printOdd(int i){
while(!oddTurn){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(type + i);
oddTurn=false;
notifyAll();
}
private synchronized void printEven(int i){
while(oddTurn){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(type + i);
oddTurn=true;
notifyAll();
}
public static void main(String[] s){
Thread odd=new Thread(new NumberPrinter("odd"));
Thread even=new Thread(new NumberPrinter("even"));
odd.start();
even.start();
}
}
Out Put:
odd1
even2
then comes to deadlock!!!!!!
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您正在等待并通知不同的对象(监视器)。
这个想法是,当您执行 objA.wait( 时,您可以调用 obj.wait() 来等待某人执行 obj.notify() ) 和
objB.notify()
。将
printOdd
方法更改为与
printEven
类似的方法。然后为
NumberPrinter
提供一个lock
对象:输出:
You're waiting and notifying different objects (monitors).
The idea is that you can call
obj.wait()
to wait for someone to doobj.notify()
, while you're doingobjA.wait()
andobjB.notify()
.Change your
printOdd
method to something likeand the
printEven
method similarly.Then provide the
NumberPrinter
with alock
object:Output:
代码中有很多错误。
首先,
synchronized
语句没有任何效果。您创建两个线程实例,每个实例仅调用自己的方法。仅当另一个线程可以调用方法时,synchronized
才有用。那么由于同样的原因,
notifyAll()
没有效果。odd.notifyAll()
无法到达挂在wait()
中的even
。因此,您需要的是另一个包含状态并且两个线程都可以看到和使用的对象。在第三个实例上使用
synchronized
、wait()
和notifyAll()
。There are a lot of bugs in the code.
First of all, the
synchronized
statements have no effect whatsoever. You create two thread instances, and each calls only its own methods.synchronized
is only useful if another thread can call a method.Then
notifyAll()
has no effect for the same reasons.odd.notifyAll()
doesn't reacheven
hanging in thewait()
.So what you need is another object which contains the state and which both threads can see and use. Use
synchronized
,wait()
andnotifyAll()
on that third instance.同样可以使用Lock接口来解决:
NaturalOrder.java
OddNumberLock.java
EvenNumberLock.java
The same can be solved using Lock interface:
NaturalOrder.java
OddNumberLock.java
EvenNumberLock.java
我认为问题可能是
printOdd
和printEven
在不同的锁(线程的对象实例锁)上同步。因此,您无法保证静态变量oddTurn
上的更改在其他线程中可见。尝试在开始时使oddTurn
不稳定。I think the problem might be that
printOdd
andprintEven
synchronize on different lock (the Thread's object instance locks). Therefor you have not guaranteed that the change on the static variableoddTurn
will be visible in the other thread. Try to make theoddTurn
volatile for the start.我这样做了
I did this way
您在
oddTurn
变量中缺少volatile
关键字。如果没有它,就无法保证线程看到实际值。You're missing
volatile
keyword withinoddTurn
variable. Without it there are no guarantees the threads see the actual value.i 使用共享对象来控制执行顺序
i Used a shared object to control the order of execution
这是我的解决方案,无需任何等待或通知。
wait() 和 notify()/notifyAll() ,
我看不出有任何理由将它们用于此问题陈述。
Here's my solution without any waits or notify.
wait() and notify()/notifyAll() ,
I dont see any reason to use them for this problem statement.
使用 Lock 界面更正您的代码:
Your code corrected with using Lock interface:
limit 5 的输出:
输入 limit: 5
奇数线程 1
偶数线程2
奇数线程 3
偶数线程 4
奇数线程 5
output for limit 5:
enter limit: 5
odd thread 1
even thread 2
odd thread 3
even thread 4
odd thread 5
我已经以这种方式实现了,根据参数,不会生成任何线程,并且不会以循环方式生成相应的线程。
即,如果线程数为 3,则线程 1 将打印 1,4 ...;线程 2 将打印 2,5,...,线程 3 将打印 3,6...
I have implemented in such a way, based on the argument, no of threads will be spawned and will the respective no in round robin manner.
i.e., If thread count is 3, thread 1 will print 1,4 ...; thread 2 will print 2,5,... and thread 3 will print 3,6...
两个线程的程序交替打印奇数和偶数。
#使用“对象锁”概念实现。
Program for Two Threads Alternatively Print Odd and Even Numbers.
#Implemented Using "Object Lock" Concept.
我用很简单的方式实现了,从1到40>
I implemented it in a very simple way, from 1 to 40>