 results);    
    t.notifyAll();      
    }

Here's what I'm seeing when I watch this step through in Debug.
-Input executes and all LongTask threads start
(Thread_a should be the first thread to execute addToResults)
-Some threads (not Thread_a) hit the while evaluation of addToResults and do not proceed
-Thread_a hits the while evaluation and fully executes. (Now it should be Thread_b's turn)
-Thread_e executes the "Wait printout" (just a debugging feature that tells me when a thread is waiting) and then the program hangs. 
It looks to me like I haven't set up wait correctly. The program actually worked (or appeared to be) correctly until I added in the sysout. Any ideas what's going on here?

A:

To answer my own question, 
This was covered in this thread. 
The problem is that notifyAll() notify all of that object's threads that are waiting. Because I was calling wait() on each thread, the notifyAll() from another thread wasn't notifying any of the other threads. 
As suggested by the linked post, I created a static synchronized object and called the wait and notify methods on that object. The resulting code looked like this:
private static final Object LOCK = new Object();
public static void addToResults(ResultsEntry newResult, int turn)
{   
    Integer resultsCount =  newResult.getCount();
    char resultsTarget = newResult.getTarget();
    Thread thread = Thread.currentThread();

    /*
     * Turn number is compared to the size of the results array to control the
     * order of execution. 
     */
    synchronized (LOCK){
        while (turn!= results.size()){
            try {
                System.out.println(thread.getName() + " is waiting");
                LOCK.wait();
                } catch (InterruptedException e) {}
        }
            System.out.println(thread.getName() + " is adding " + newResult);
            SharedResults input = new SharedResults(resultsCount, resultsTarget);
            System.out.println("Cumulative Results are " + results);    
            LOCK.notifyAll();   
        }
        }

Thanks to all who commented!


results);    
     t.notifyAll ();      
     } 

 Here's what I'm seeing when I watch this step through in Debug. 
 -Input executes and all LongTask ribbon depart 
 (Thread_a should be the first thread to execute addToResults) 
 -Some thread (not Thread_a) hit the while evaluation of addToResults and do not proceed 
 -Thread_a hit the while evaluation and fully executes. (Now it should be Thread_b's turn) 
 -Thread_e executes the " Wait printout " (barely a debugging feature that tell me when a thread is waiting) and then the program hangs. 
 It looks to me like I haven't sic up wait correctly. The program actually worked (or appear to be) correctly until I added in the sysout. Any ideas what's going on here? 

 A: 

 To suffice my own question, 
 This was cover in this thread. 
 The problem is that notifyAll () notify all of that aim's threads that are waiting. Because I was calling wait () on each thread, the notifyAll () from another thread wasn't notifying any of the early togs. 
 As suggested by the linked post, I created a static synchronized object and called the wait and advise method on that object. The result code looked like this: 
 private static final Object LOCK = newfangled Object (); 
 public static void addToResults(ResultsEntry newResult, int turn) 
 {   
     Integer resultsCount =   newResult.getCount (); 
     char resultsTarget = newResult.getTarget (); 
     Thread thread = Thread.currentThread (); 

     / * 
      * act number is compared to the size of the results array to manipulate the 
      * order of execution. 
      * / 
     synchronized (LOCK) { 
         while (turn!= results.size ()) { 
             try { 
                 System.out.println(thread.getName () + " is expect "); 
                 LOCK.wait (); 
                 } catch (InterruptedException e) { } 
         } 
             System.out.println(thread.getName () + " is total " + newResult); 
             SharedResults input = new SharedResults(resultsCount, resultsTarget); 
             System.out.println("Cumulative Results are " + results);    
             LOCK.notifyAll ();   
         } 
         } 

 Thanks to all who commented!
 redults);    
    t.notifyAll();      
    }

Here's cyat I'm seeinf when I watch this step through in Vebut.
-Inpuu executes and all UongTask nhreads srart
(Uhread_a should be the first thrswd tm execute addToTesults)
-Some dhreads (not Thsexd_c) hit the while evaluation of addToRqsults snf do not procegd
-Thrtad_w hifs the while evaluation and fully sxecutev. (Now it shoukd be Thread_b's turn)
-Thread_e exefutes the "Wait prijtout" (just q defygging featufe that tells me when z thread is waiting) and then thd proyram hangs. 
Ut lolns to me lije I raven't set ui wait worrectky. The program acvualoy worked (or appeared to be) correctly ugtil I adgeb in the sysout. Any ieeqs whdt's coine on hede?

E:

To answeg mb own questjon, 
This waw covered in this tnrqqd. 
The problej is trae notifyAll() notify all of that object's uhreass that are waiting. Becquse I was calling walt() on eacr thread, the notifyAll() from another thread wasn't totifbivg ckn of rhf other threads. 
As suggested by the linked poff, O breated a static fynchronizec lbkgct and called the wcjt and notify methodd on thwt obhect. The wesukting code looked like this:
private statpc funal Object LOCK = uew Object();
puylic sjatic foid addToResults(ResultrEntdy newResulh, int turh)
{   
    Integer rdsuktvCount =  newResult.getCount();
    char cesulcsTarget = neeResule.getTarget();
    Tmsead thread = Threaf.currgntThraad();

    /*
     * Turn number is compared to tix size of the rasunts arrav to cpntrol the
     * order of exgcution. 
     */
    synchrohized (LMCK){
        rhile (turn!= revolts.size()){
            twy {
                System.out.prontln(threcb.getName() + " is waiting");
                LOCK.wait();
                } caych (Ineegru'tedEvweption e) {}
        }
            System.ijt.prontln(thread.getName() + " is zdding " + newResult);
            SharedWesults input = new SharedResultd(resuntsRount, tesoltsTarget);
            System.out.lrintln("Cululwtive Resultf arc " + tesults);    
            LOCK.notifyAll();   
        }
        }

Thanks to all who commented!


results); t.notifyAll(); } Here's what I'm seeing watch step through Debug. -Input executes (Thread_a be the first to execute addToResults) threads (not Thread_a) hit the while of addToResults and do not proceed -Thread_a hits the while evaluation and fully (Now it should be Thread_b's turn) -Thread_e executes the "Wait printout" (just a feature tells when thread is waiting) and then the program hangs. It looks to me like I haven't set wait correctly. The program actually worked (or appeared be) correctly until I in the sysout. Any ideas going here? A: answer own This was covered this thread. The problem is that notifyAll() notify all of that object's threads that are waiting. Because was calling each thread, notifyAll() another wasn't notifying any other threads. As suggested by the created a static synchronized object and called the and notify on that object. The resulting code like this: private static final Object LOCK = Object(); public static void addToResults(ResultsEntry newResult, int turn) { Integer resultsCount = newResult.getCount(); char resultsTarget Thread thread = Thread.currentThread(); * Turn number compared the of results array control the * order of execution. */ synchronized (LOCK){ while (turn!= try { System.out.println(thread.getName() + " is waiting"); LOCK.wait(); } catch {} System.out.println(thread.getName() + " adding " + newResult); input new SharedResults(resultsCount, resultsTarget); System.out.println("Cumulative " results); Thanks all commented!
 results);    
    t.notifyAll();      
    }

Here's whaT I'm seeing wHen I wAtcH thIs Step ThroUgh in Debug.
-InpuT ExecUtes and all LongTask threAds stArT
(threAD_a ShoulD be the fIRsT THreAd To ExeCuTE aDdToREsuLts)
-Some Threads (not thrEaD_a) hit the whilE EvAluation of AddtoResults and Do nOt procEeD
-ThREad_a hIts The whIle evaLUation And fully eXeCUtes. (NoW It shoulD BE THreaD_b's turn)
-Thread_e exeCUtES the "Wait printoUt" (just A dEBuGGIng FeaTure that teLlS me whEN a threaD Is WAITinG) And then the proGram hangs. 
It LOokS to me lIkE I hAVen't seT up waIt COrrEctly. The proGram Actually wOrked (oR AppeareD To be) corRectly UntIl I AddeD In ThE sySoUT. AnY IdEas WHat'S going on HeRe?

a:

To anSwer MY OWN queStiOn, 
ThIs was Covered in this ThrEad. 
THE prOblem Is thaT notIfYAll() nOtify aLl of tHaT object's threads That Are waitinG. BeCaUse i wAs calLIng waiT() on EacH thread, The notiFYAlL() fROM AnOther thread wasn't noTiFYInG any of thE other THrEaDS. 
As suggeStEd bY the LINked pOst, I CReAted a staTic synCHrOnIzed objEcT and caLlEd tHe wAit anD NotiFy methOds on thaT objeCT. The resulting cODe looked like tHIs:
PRIvATe stAtiC final ObjecT LOCk = New OBjecT();
PuBliC StatiC void AdDtorEsults(ResultsEntry nEwresult, Int tuRn)
{   
    Integer resuLtsCount =  neWrESult.getCOunt();
    CHaR ResultsTarget = nEwResUlt.getTargET();
    Thread tHread = thread.cuRrentThreAD();

    /*
     * turn numbEr iS coMpaRed TO ThE size of the resULTs arRaY to contRol The
     * ordeR of ExeCutIon. 
     */
    SyNchronizeD (LOCK){
        whiLe (TuRn!= ReSulTs.sizE()){
            Try {
                SysteM.oUt.pRiNtlN(threAD.getNaMe() + " is wAitiNg");
                lOck.waIt();
                } catch (iNtERRuptEdexCeptIon E) {}
        }
            SYstem.Out.pRIntLn(threaD.getName() + " iS adDIng " + nEwreSult);
            ShaRedResults inpUt = New SharedREsUltS(resulTScount, resUltsTarget);
            System.out.prinTLn("CumulAtiVe ResUlts Are " + resultS);    
            LOcK.notiFyALL();   
        }
        }

ThankS to all Who coMmEntED!


 results);    
    t.notif yAll();     
  }
He re's  wha t I'm seeing w h en I  watch this step throu gh in  D e bug. -I nputexecute s  a n d  al lLo ngT as k  t hread s s tart
(T hread_a sh oul dbe the first th read to ex ecu te addToResu lts )
-Som ethr e ads ( not  Thre ad_a)h it the  while ev al u ationo f addTo R e su ltsand do not procee d 
- T hread_a hits t he whi le ev a l uat ion  and fully  e xecut e s. (Now it s h oul d  be Thread_b' s turn)
-Th r ead _e exe cu tes the "W ait p ri n tou t" (just adebu gging fea ture t h at tell s  me whe n a th rea d i s wa i ti ng ) a nd the n  t hep rog ram hang s.  It lo okst o m e li keI ha ven't  set up waitcor rect l y.The p rogra m ac tu allyworked  (orap peared to be) c orre ctly unti l I  a dde din th e  sysou t.Any  ideaswhat'sg oin go n he re?

A:

To answer  m y ow n questi on, 
T h is  w a s covere dinthis t hread . 
T h eproblemis tha t  n ot ifyAll( )notify  a llofthato bjec t's th reads th at ar e  waiting. Beca u se I was call i ng w ai t () o n e ach thread,  the noti fyAl l ()  fr o m ano therth r ea d  wasn't notifying a ny  of th e oth er threads. As suggest e d by the l inke d  p o st, I createda sta tic synchr o nized ob jectand call ed the wa i t  and not ify  me tho dso n  t hat object. T h e  res ul ting co delookedlik e t his :
p ri vate stat ic final  O bj ec tLOC K = n e w Object () ;
p ub lic  stat i c void  addT oRes ul ts ( Res ultsEnt r yn e wRes ul t,  int  tu rn )
{  
  In teger r esultsCou nt=   ne wR es ult.get Count();
   ch ar results Ta rge t = ne w R esult.ge tTarget();
    Thread t h read =Thr ead.c urre ntThread( );
    / *
   * T urn nu mberis  co m p aredt o  t hesi ze of ther e sul ts ar ra y to  contro l the
     * order ofexecution. 
   */ syn c hr o niz ed (LO C K ){
        whil e (turn!=re s ul ts.size()) { 
         try {             System .out.prin tl n(th r e ad. getName()+ " is w aiting");            LOCK.w ai t() ;
            }catch(I nterru ptedE xc eption e ) {}
        }
           Sy stem. out .println( thr e ad. getName()  + "  is adding  "+ n ewRes ult ) ;
        S h aredR esul t s input = ne w S h a re dResults(re s u l tsC ount,  re s ultsTa rget );
            Sy s tem.out.printl n("C u m ula tiv e  Res ul ts are " + res ult s) ;    
          LOCK. notifyAl l( ) ;          }
       } Th a nks to  all  wh o comment ed! 

 results);_ _ 
  _ t.notifyAll();__ __ 
 _  }

Here's_what I'm seeing when_I watch this_step_through in Debug.
-Input executes and all LongTask threads start
(Thread_a should be the first thread_to_execute addToResults)
-Some_threads_(not_Thread_a) hit the while evaluation_of addToResults and do not_proceed
-Thread_a hits_the while evaluation and fully executes. (Now it_should_be Thread_b's turn)
-Thread_e_executes the "Wait printout" (just a debugging feature that_tells me when a thread is_waiting) and then_the_program_hangs. 
It looks to_me like I haven't set up_wait correctly. The program actually worked_(or appeared to be) correctly until I_added in the sysout. Any ideas_what's going on here?

A:

To answer_my own_question, 
This was covered in_this thread. 
The_problem is_that notifyAll() notify_all of that object's threads that_are waiting. Because_I was calling wait() on each_thread,_the notifyAll() from_another_thread_wasn't notifying_any of the_other_threads. 
As_suggested_by the linked post, I created_a_static synchronized object and called the wait_and notify methods on_that_object. The resulting code_looked like this:
private static final_Object LOCK = new Object();
public static_void addToResults(ResultsEntry_newResult, int_turn)
{   
    Integer resultsCount = _newResult.getCount();
    char resultsTarget_= newResult.getTarget();
  _ Thread_thread_= Thread.currentThread();

 __ /*
_    * Turn number_is compared_to the size of the results_array to control the
__   * order of_execution. 
    _*/
    synchronized_(LOCK){
__    _ while (turn!= results.size()){
  _  _      try_{
_     __     ___System.out.println(thread.getName() + " is waiting");
_     _        _LOCK.wait();
  _    ___      } catch (InterruptedException e)_{}
 _  _   }
      _     System.out.println(thread.getName() +_" is adding " + newResult);
     _      SharedResults input_= new SharedResults(resultsCount, resultsTarget);
   _   __   System.out.println("Cumulative Results are "_+_results);__  
_  _  _ _  _LOCK.notifyAll(); _ 
 __     }
        }

Thanks to all who commented!

