java - Multiple thread to print numbers -


i'm student trying learn java. want print employee id 1 100 using 3 threads. used code shown below.

my main class:

public class mainthread {      public static void main(string[] args) {         printthread pr = new printthread();         thread t1 = new thread(pr);         thread t2 = new thread(pr);         thread t3 = new thread(pr);         t1.start();         t2.start();         t3.start();     } }  public class printthread extends thread {      private final object sync = new object();      public void run() {         synchronized (sync) {             (int = 1; <= 101; i++) {                 system.out.println("employee id : " + i);                 try {                     thread.sleep(100);                  } catch (interruptedexception e) {                     e.printstacktrace();                 }             }         }      }  } 

i'm getting output 1....100 printed thread 1 again thread 2 , thread 3.

but intended output thread 1 printing one,thread 2 printing 2,thread 3 printing 3 , on.

please me sort out.!!!

there several issues code. fundamental 1 threads not told portion of work do. if want split work chunks each task, you'd have tell each thread portion of work do. since have stated want thread 1 produce output 1, 4, 7, …, we'd tell each thread start, stop , how advance counter in each iteration.

before i'll show code this, let's @ other problems need fixing. synchronization flawed 2 reasons. first, every printthread has own sync monitor. therefore, locking useless. you'll need monitor shared threads need synchronized.

second, you've put entire loop (including sleep) inside synchronized block. means if monitor shared between threads, program sequential. usually, want synchronize as little possible in order keep concurrency @ maximum. in case, thing needs synchronization printing (so not mess single unrelated lines of output, eg "12\n3\n" instead of "1\n23\n"). in such case, want use concurrent object monitor. here, pass printstream constructor , use monitor.

another thing needs attention handling of interruptedexception. such exception thrown signal thread (whoever is) have lot interest in operation if please kind exit gracefully @ earliest convenience. proper handling of catching interruptedexception therefore clean needs cleaning , return possible.

finally, not style inherit thread. instead, implement runnable , pass instances of class constructor of thread. (but if do inherit thread, don't wrap instance yet another thread.)

putting together, following:

import java.io.printstream;  public class printingtask implements runnable {      private final printstream stream;     private final int first;     private final int last;     private final int incr;      public printingtask(final printstream stream,                         final int first,                         final int last,                         final int incr) {         if (stream == null) {             throw new nullpointerexception("stream");         }         this.stream = stream;         this.first = first;         this.last = last;         this.incr = incr;     }      @override     public void run() {         (int = this.first; <= this.last; i+= this.incr) {             synchronized (this.stream) {                 this.stream.println(i);             }             try {                 thread.sleep(100);             } catch (final interruptedexception e) {                 return;             }         }     } } 

then can use so:

public static void main(string[] args) throws interruptedexception {     final int nthreads = integer.parseint(args[0]);     final thread[] workers = new thread[nthreads];     (int = 0; < nthreads; ++i) {         workers[i] = new thread(new printingtask(system.out, + 1, 100, nthreads));     }     (final thread t : workers) {         t.start();     }     (final thread t : workers) {         t.join();     } } 

it print arbitrary interleaving of lines numbers 1 100 each line printed atomically. hope want.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -