java - Sequence number using thread Synchronization -
i want print series of 1 100 number using n number of threads (lets take 10 threads this). condition 1st thread have sequence number 1, 11,21....91, 2nd thread have sequence 2,12,22.....92 , on. other threads have sequence number that. want print number in sequence 1 100. know can use synchronization, wait , notify method , using variable or flag counter don't think idea use it. want use without concurrency (like executors etc) how that. please suggest.
public class printnumbersequenceusingrunnable { int notifyvalue = 1; public static void main(string[] args) { printnumbersequenceusingrunnable sequence = new printnumbersequenceusingrunnable(); thread f = new thread(new first(sequence), "fisrt"); thread s = new thread(new second(sequence), "second"); thread t = new thread(new third(sequence), "third"); f.start(); s.start(); t.start(); } } class first implements runnable { printnumbersequenceusingrunnable sequence; public first(printnumbersequenceusingrunnable sequence) { this.sequence = sequence; } @override public void run() { printfist(); } private void printfist() { synchronized (sequence) { (int = 1; <= 20; += 3) { while (sequence.notifyvalue != 1) { try { sequence.wait(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } system.out.println(thread.currentthread().getname() + " " + i); sequence.notifyvalue = 2; sequence.notifyall(); } } } } class second implements runnable { printnumbersequenceusingrunnable sequence; public second(printnumbersequenceusingrunnable sequence) { this.sequence = sequence; } @override public void run() { printsecond(); } private void printsecond() { synchronized (sequence) { (int = 2; <= 20; += 3) { while (sequence.notifyvalue != 2) { try { sequence.wait(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } system.out.println(thread.currentthread().getname() + " " + i); sequence.notifyvalue = 3; sequence.notifyall(); } } } } class third implements runnable { printnumbersequenceusingrunnable sequence; public third(printnumbersequenceusingrunnable sequence) { this.sequence = sequence; } @override public void run() { printthrid(); } private void printthrid() { synchronized (sequence) { (int = 3; <= 20; += 3) { while (sequence.notifyvalue != 3) { try { sequence.wait(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } system.out.println(thread.currentthread().getname() + " " + i); sequence.notifyvalue = 1; sequence.notifyall(); } } } }
you need have values sorted on each threads. each time thread writes number, triggers event in event bus. threads subscribed event.
you start system triggering event [minimum value - 1].
each thread receive notification value [minimum value - 1] has been published. thread has value [minimum value] act , trigger new event value [minimum value + 1].
edit: haven't tested it, this.
static void main(string[] args) { list<deque<integer>> publishqueues = new arraylist<>(); (int = 1; <= 10; i++) { new thread(new worker(i, publishqueues)).start(); } } class worker implements runnable { deque subscriberqueue; list<deque<integer>> publishqueues; int i; worker(int i, list<deque<integer>> publishqueues) { this.i = i; this.publishqueues = publishqueues; this.subscriberqueue = new concurrentlinkeddeque<>(); this.publishqueues.add(this.subscriberqueue); } void run() { linkedlist<integer> ints = new linkedlist<>(); (int j = i; j <= 100; j+=10) { ints.add(j); } while (true) { integer publishedinteger = subscriberqueue.poll(); if (publishedinteger == ints.getfirst() - 1) { integer integer = ints.poll(); system.out.println(integer); (dequeu<integer> publishqueue : publishqueues) { publishqueue.addlast(integer); } } } } }
Comments
Post a Comment