0
我想運行三個線程奇數(打印奇數否),偶數(打印偶數)和SUM(打印他們的SUM),代碼工作正常,但程序沒有終止。即使線程仍在等待想運行三個奇數偶數和總和
package viv.tyagi.EvenOddThrad.EvenOddAndSumCorrectVersion;
public class EvenOddAndSum
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
EvenOdd obj = new EvenOdd();
Even e = new Even(obj);
Odd o = new Odd(obj);
Sum s = new Sum(obj);
Thread oddThraed= new Thread(o,"Odd");
Thread evenThraed= new Thread(e,"Even");
Thread sumThraed= new Thread(s,"SUM");
oddThraed.start();
evenThraed.start();
sumThraed.start();
}
}
class EvenOdd
{
int seq;
int evenNo;
int oddNo=-1;
int sum=0; // 0 for odd , 1 for even , 2 for sum
public synchronized int getEvenNo()
{
System.out.println(Thread.currentThread().getName()+"Before seq"+seq);
while (true)
{
System.out.println(Thread.currentThread().getName() + "waiting ...." + "seq is " + seq);
if (seq == 1)
{
break;
}
else
{
try
{
wait();
}
catch (Exception e)
{
// TODO: handle exception
}
}
System.out.println(Thread.currentThread().getName() + "released ...." + "seq is " + seq);
}
System.out.println(Thread.currentThread().getName()+"After seq"+seq);
evenNo=evenNo+2;
System.out.println(Thread.currentThread().getName()+" even---------------------------------------->"+evenNo);
seq=2;
notifyAll();
return evenNo;
}
public synchronized int getOddNo()
{
System.out.println(Thread.currentThread().getName()+"Before seq"+seq);
while (true)
{
System.out.println(Thread.currentThread().getName() + "waiting ...." + "seq is " + seq);
if (seq == 0)
{
break;
}
else
{
try
{
wait();
}
catch (Exception e)
{
// TODO: handle exception
}
}
System.out.println(Thread.currentThread().getName() + "released ...." + "seq is " + seq);
}
System.out.println(Thread.currentThread().getName()+"After seq"+seq);
oddNo=oddNo+2;
System.out.println(Thread.currentThread().getName()+" odd---------------------------------------->"+oddNo);
seq=1;
notifyAll();
return oddNo;
}
public synchronized int getSum()
{
System.out.println(Thread.currentThread().getName()+"Before seq"+seq);
while (true)
{
System.out.println(Thread.currentThread().getName() + "waiting ...." + "seq is " + seq);
if (seq == 2)
{
break;
}
else
{
try
{
wait();
}
catch (Exception e)
{
// TODO: handle exception
}
}
System.out.println(Thread.currentThread().getName() + "released ...." + "seq is " + seq);
}
System.out.println(Thread.currentThread().getName()+"After seq"+seq);
sum=oddNo+evenNo;
System.out.println(Thread.currentThread().getName()+" sum---------------------------------------->"+sum);
seq=0;
notifyAll();
return sum;
}
}
class Even implements Runnable
{
EvenOdd obj;
Even(EvenOdd obj)
{
this.obj=obj;
}
public void run()
{
for(int i=0;i<10;i++)
{
obj.getEvenNo();
try{
// Thread.sleep(1000);
}catch (Exception e) {
// TODO: handle exception
}
}
}
}
class Odd implements Runnable
{
EvenOdd obj;
Odd(EvenOdd obj)
{
this.obj=obj;
}
public void run()
{
for(int j=1;j<10;j++)
{
obj.getOddNo();
}
}
}
class Sum implements Runnable
{
EvenOdd obj;
Sum(EvenOdd obj)
{
this.obj=obj;
}
public void run()
{
for(int k=1;k<10;k++)
{
obj.getSum();
}
}
}
謝謝,對我來說,它看起來有些線程通知問題,但它是一個簡單的事情 – 2014-10-20 14:16:20