multithreading - Java MulthiThreading Equal Cards divide with 4 threads -


I am trying to learn multi-threading by implementing the code snippet. The problem is that using the four lists (13 each) to distribute the card (52) between the four lists, please suggest a better solution or improvement in the code given below.

Because it is for practice on multi-threading, I have not been stressed on conferences and generic nomenclature (apology for this)

  import java.util .LinkedList; Import java.util.list; Public class carddivideinterfire {static final int max = 52; Static Int Val = 0; Static object ob = new object (); Fixed list list 1 = new linked list (); Fixed list list 2 = new linked list (); Fixed list list 3 = new linked list (); Fixed list list 4 = new linked list (); Public Static Zero (String [] Args) {Thread T1 = New Thread (New Runnabal) (Public Zero Free) {While (true) {If (Val> = Max) {Brake}} Sync (bull) {List1.add (++ val); System.out.println ("a & gt;" + val); Ox.notifyAll (); Try {if (val> = max) {// System.out.println ("t1" expired "); // thread. CurrentThread (). Interrupt (); break;} thread. Sleep (1000 ); Ox.wait ();} hold (blocked expression e) {}}} // indispensable code // system .out.println ("run 1 run");}}); thread t2 = new thread ( New Runnabal) (Public Zero Void) {When (true) {If (Val> gt; Max} {Brake;} Syncing (Obs) {list2.add (++ val); System Out.println ("b" "Val"); ox.notifyAll (); Try {if (val> = max) {break;} thread, sleep (1000); ox.wait ();} hold (interrupted;) {}}} }}); Thread t3 = new thread (new Runnable (non-zero) (when the public is not) {when (true) {if (val> = max) {brake;} syncing (obs) {list3.add (++ val); System Out.println ("c & gt;" + Val; ox.notifyAll (); Try {if (val> = max) {break;} thread, sleep (1000); ox.wait ();} hold (blocked exception e) {}}}} }); Thread t4 = new thread (new runanble) {public zero-zero} {when (true) {if (val> = max) {break}} synchronize (bull) {list4.add (++ val); system Out.println ("d & gt;" + val); Ox.notifyAll (); {If (val> = max) {break;} thread, sleep (1000); Ox.wait ();} catch (interrupted;) {}}}}}); T1.start (); T2.start (); T3.start (); T4.start (); Try {t1.join (); T2.join (); T3.join (); T4.join (); } Hold (exception e) {} System.out.print ("list is 1">); For (object o: list 1) {System.out.print ((integer) o + ","); } System.out.println (""); System.out.print ("The list is 2";); For (object o: list 2) {System.out.print ((integer) O + ","); } System.out.println (""); System.out.print ("The list is 3";); (Object o: list 3) for {System.out.print ((integer) O + ","); } System.out.println (""); System.out.print ("The list is 4";); For (object o: list 4) {System.out.print ((integer) O + ","); }}}}    

Why invent the wheel again? The new Java SE7 provides you, this work is concurrent for you.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -