2014-10-19 59 views
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(); 
     } 
    } 
} 

回答

0

Even線環10次(i = 0..9),而其它僅9次(J/K = 1..9)。因此,它等待(永遠)其他線程告訴他輪到他了(通過設置seq=1notifyAll),但這絕不會發生,因爲其他線程已經終止。您將需要修復循環限制。

+0

謝謝,對我來說,它看起來有些線程通知問題,但它是一個簡單的事情 – 2014-10-20 14:16:20