Multi threaded java program to print even and odd numbers alternatively
Tag : java , By : hyperNURb
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Well, there are many alternatives. I would probably use a SynchronousQueue instead (I don't like low-level wait/notify and try to use higher-level concurrency primitives instead). Also printOdd and printEven could be merged into single method and no additional flags are necessary: public class App {
static class OddEven implements Runnable {
private final SynchronousQueue<Integer> queue = new SynchronousQueue<>();
public void start() throws InterruptedException {
Thread oddThread = new Thread(this);
Thread evenThread = new Thread(this);
oddThread.start();
queue.put(1);
evenThread.start();
}
@Override
public void run() {
try {
while (true) {
int i = queue.take();
System.out.println(i + " (" + Thread.currentThread() + ")");
if (i == 10)
break;
queue.put(++i);
if (i == 10)
break;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws InterruptedException {
new OddEven().start();
}
}
|
[Print alternatively using alternate Threads]
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You will need the highest Id given to your threads. After this, you can simplify your code using the modulo operator. class Display implements Runnable {
int threadId;
static int v = 1;
static int higehstId = 0;
static Object monitor = new Object();
Display(int id) {
this.threadId = id;
}
public void run() {
print();
}
private void print() {
while (v < 100) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (monitor) {
if ((v - 1) % higehstId == threadId - 1) {
System.out.println(threadId + ":" + v);
v++;
monitor.notifyAll();
} else {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] ar) {
Thread t1 = new Thread(new Display(1));
Thread t2 = new Thread(new Display(2));
Thread t3 = new Thread(new Display(3));
Thread t4 = new Thread(new Display(4));
Thread t5 = new Thread(new Display(5));
Display.higehstId = 5;
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
|
(Multi-Threading Python) Print 1 to 10 numbers alternatively, using two threads
Tag : python , By : firebasket
Date : March 29 2020, 07:55 AM
I hope this helps you . It is rarely useful to have threads move in lockstep with each other. If you really needed them to, you could use two separate events, as alluded to by Prudhvi. For example: from threading import Thread, Event
event_1 = Event()
event_2 = Event()
def func_1(ev1, ev2):
for i in range(1, 11, 2):
ev2.wait() # wait for the second thread to hand over
ev2.clear() # clear the flag
print('1')
ev1.set() # wake the second thread
def func_2(ev1, ev2):
for i in range(2, 11, 2):
ev2.set() # wake the first thread
ev1.wait() # wait for the first thread to hand over
ev1.clear() # clear the flag
print('2')
t1 = Thread(target=func_1, args=(event_1, event_2))
t2 = Thread(target=func_2, args=(event_1, event_2))
t1.start()
t2.start()
|
program to print odd numbers and even numbers on seperate threads
Date : March 29 2020, 07:55 AM
|
How to create 2 threads and to print Thread id's alternatively and numbers from 1 to 20?
Tag : c , By : firebasket
Date : March 29 2020, 07:55 AM
|