I am trying a write a simple program with wait and notify in which I will create 3 threads.
The first thread should print 1, 2, 3.
The second thread should print 4, 5, 6.
The third thread should print 7, 8, 9.
After that, the first thread should print 10, 11, 12 and so on.
Below is a sample code for the same exercise, but I am unable to print the desired output.
public class MyThread2 extends Thread {
public final static Object obj = new Object();
int threadNo;
static volatile int threadNoToRun;
static volatile int counter = 1;
public MyThread2(int threadNo){
this.threadNo= threadNo;
}
#Override
public void run() {
synchronized (obj) {
try {
if(threadNoToRun != threadNo)
obj.wait();
else{
for(int i = 1 ; i < 4 ; i++){
if(threadNoToRun == threadNo){
System.out.println(threadNo + " counter value is "+counter);
counter++;
System.out.println(threadNo + " counter value is "+counter);
counter++;
System.out.println(threadNo + " counter value is "+counter);
counter++;
if(threadNoToRun == 1){
threadNoToRun = 2;
}
else if(threadNoToRun == 2){
threadNoToRun = 3;
}
else if(threadNoToRun == 3){
threadNoToRun = 1;
}
}
}
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main (String args[]) {
/*
* Creating as many threads as needed.
*/
MyThread2 th1 = new MyThread2(1);
MyThread2 th2 = new MyThread2(2);
MyThread2 th3 = new MyThread2(3);
MyThread2.threadNoToRun = 1;
th1.start();
th2.start();
th3.start();
}
}
The output looks like:
1 counter value is 1
1 counter value is 2
1 counter value is 3
2 counter value is 4
2 counter value is 5
2 counter value is 6
Here, it was just a few changes.
Nonetheless, I have to point out that this kind of concurrency does NOT increase computation speed. Only one thread is alive at all times.
public class MyThread2 extends Thread {
public final static Object obj = new Object();
int threadNo;
static volatile int threadNoToRun;
static volatile int counter = 1;
public MyThread2(int threadNo) {
this.threadNo = threadNo;
}
#Override
public void run() {
synchronized (obj) {
try {
while (counter < 100) {
if (threadNoToRun != threadNo)
obj.wait();
else {
System.out.println(threadNo + " counter value is " + counter);
counter++;
System.out.println(threadNo + " counter value is " + counter);
counter++;
System.out.println(threadNo + " counter value is " + counter);
counter++;
if (threadNoToRun == 1) {
threadNoToRun = 2;
} else if (threadNoToRun == 2) {
threadNoToRun = 3;
} else if (threadNoToRun == 3) {
threadNoToRun = 1;
}
obj.notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
/*
* Creating as many threads as needed.
*/
MyThread2 th1 = new MyThread2(1);
MyThread2 th2 = new MyThread2(2);
MyThread2 th3 = new MyThread2(3);
MyThread2.threadNoToRun = 1;
th1.start();
th2.start();
th3.start();
}
}
I'm learning JUC,I want to calculate the total time that the program runs with five threads,but it's blocked after print "1 2 3"。please tell me what the reason is ?
In addition, if I don't call the function "isPrime(int)",The program will execute normally.
public class TestCountDownLatch {
public static void main(String[] args) {
CountDownLatch cwt = new CountDownLatch(5);
Runnable runnable = new CountDownThread(cwt);
long start = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
new Thread(runnable).start();
}
try {
cwt.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("total time :" + (end - start));
}
}
class CountDownThread implements Runnable{
private CountDownLatch countdownLatch;
private int num = 1;
public CountDownThread(CountDownLatch countdownLatch) {
this.countdownLatch = countdownLatch;
}
#Override
public void run() {
try{
while(true){
synchronized (this) {
if(num > 100){
break;
}
if(isPrime(num)){
System.out.println(num++);
}
}
}
}finally{
countdownLatch.countDown();
}
}
private boolean isPrime(int i) {
for (int j = 2; j <= (i >> 1); j++) {
if(i % j == 0){
return false;
}
}
return true;
}
}
Your Runnable run method, is only incrementing num when its prime and hence when it encounters 4 which is not prime its not incrementing num and your program is in that state for rest of period when its running. Fiddled with below mentioned piece which makes it go beyond that point and break at 100.
#Override
public void run() {
try {
while (true) {
synchronized (this) {
num++; // initially assigning int num = 0, and then doing this
if (num > 100) {
break;
}
if (isPrime(num)) {
System.out.println(num);
}
}
}
} finally {
countdownLatch.countDown();
}
}
I'm new to muti-threading and I got a question to print 1 to 100 using 10 threads in Java with below constrain.
Thread t1 should print:
1, 11, 21, 31, ... 91
t2 should print:
2, 12, 22, 32, ... 92
likewise
t10 should print:
10, 20, 30, ... 100
The final output should be
1 2 3 .. 100
I have tried it, but it is throwing the following exception in all 10 threads:
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at thread.run(MyThread.java:58)
at java.lang.Thread.run(Unknown Source)
Please let me know how I can solve this problem.
public class MyThread {
/**
* #param args
*/
public static void main(String[] args) {
thread.setSequence();
for(int i = 1; i <= 10; i++) {
Thread t = new Thread(new thread(i));
t.setName(i + "");
t.start();
}
}
}
class thread implements Runnable {
private static HashMap< String, String> sequence = new HashMap<String, String>();
public static final Object lock = new Object();
public static String turn = "1";
private int startValue = 0;
private AtomicInteger counter = new AtomicInteger(1);
public thread(int startValue){
this.startValue = startValue;
}
#Override
public void run() {
while (!counter.equals(10)){
synchronized (lock) {
if(Thread.currentThread().getName().equals(turn)){
System.out.print(startValue + " ");
startValue += 10;
counter.incrementAndGet();
turn = getNextTurn(turn);
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
}
}
}
public static void setSequence(){
for (int i = 1; i <= 10; i++)
if (i == 10)
sequence.put(i + "", 1 + "");
else
sequence.put(i + "", (i + 1) + "");
}
public static String getNextTurn(String currentTurn){
return sequence.get(currentTurn);
}
}
The simplest way would be to have a volatile variable from which each thread reads in and update according to its turn, otherwise it just waits until his turn. When counter is equals to 100 you stop all threads to run by breaking the outer loop.
class MyRunnable implements Runnable {
private static final int LIMIT = 20;
private static volatile int counter = 0;
private int id;
public MyRunnable(int id) {
this.id = id;
}
#Override
public void run() {
outer:
while(counter < LIMIT) {
while (counter % NB_THREADS != id) {
if(counter == LIMIT) break outer;
}
System.out.println("Thread "+Thread.currentThread().getName()+ " printed " + counter);
counter += 1;
}
}
}
Given a LIMIT of 20 and 10 threads, it outputs:
Thread 0 printed 0
Thread 1 printed 1
Thread 2 printed 2
Thread 3 printed 3
Thread 4 printed 4
Thread 5 printed 5
Thread 6 printed 6
Thread 7 printed 7
Thread 8 printed 8
Thread 9 printed 9
Thread 0 printed 10
Thread 1 printed 11
Thread 2 printed 12
Thread 3 printed 13
Thread 4 printed 14
Thread 5 printed 15
Thread 6 printed 16
Thread 7 printed 17
Thread 8 printed 18
Thread 9 printed 19
Of course, this is a very bad usage of multithreading because each thread waits its turn to print and increment the counter.
Multithreading works well when threads can work independently of another for relatively long time's window, and then may occasionally meet up to compare or combine their results if needed.
For example in the fork-join model, each thread does its task independently then their results are merged to produce the final outcome, such as in a merge sort for example. But this assume that the task can be easily parallelizable into independant subtasks, which is not the case here because your final output should be consecutive numbers.
So here a simple loop would be largely more efficient, but I can understand it's for learning purposes.
Here is a solution for the problem.The current thread acquire the lock and we decide if the thread is eligible to execute (printing the number here). If so perform the operation and notify all threads that they can try now. Else wait till its notified by other threads.
public class MyThread extends Thread{
//define the Total No.Of Threads needed
public static final int TOTAL_THREADS = 10;
public final static Object obj = new Object();
int threadNo;
static volatile int counter = 1;
public MyThread(int threadNo){
this.threadNo= threadNo;
}
#Override
public void run(){
//in a synchronized block to acquire lock
synchronized (obj) {
while(counter<=100){
/*
* counter==threadNo => To print the initial numbers till TOTAL_THREADS
* counter%TOTAL_THREADS == threadNo => e.g 11%10 = 1 -> 1 will print this, 12%10 = 2 ..
* (counter%TOTAL_THREADS == 0) && (TOTAL_THREADS == threadNo) => 10%10 will be 0,
* and this must be printed by 10 th thread only, ie the highest thread.
*/
if(counter == threadNo || (counter%TOTAL_THREADS == threadNo) ||
((counter%TOTAL_THREADS == 0) && (TOTAL_THREADS == threadNo))){
//Display the output as desired
System.out.println(this.threadNo+" printing"+" "+counter++);
//notify
obj.notifyAll();
}else{
//current thread not eligible for printing the current counter value, so wait till its notified
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main (String args[]) {
/*
* Creating as many threads as needed.
*/
for(int i = 1; i<=TOTAL_THREADS;i++){
MyThread th = new MyThread(i);
th.start();
}
}
}
The output will be
1 printing 1,
2 printing 2,
3 printing 3,
4 printing 4,
5 printing 5,
6 printing 6,
7 printing 7,
8 printing 8,
9 printing 9,
10 printing 10,
1 printing 11,
2 printing 12,
3 printing 13,
4 printing 14,
...
7 printing 97,
8 printing 98,
9 printing 99,
10 printing 100
Hope this helps =) Took me an hour to do it.
package com.xxxx.simpleapp;
import java.util.ArrayList;
import java.util.List;
public class TenThreads {
public int currentTaskValue = 1;
public static void main(String[] args) {
TenThreads monitor = new TenThreads();
List<ModThread> list = new ArrayList();
for (int i = 0; i < 10; i++) {
ModThread modThread = new ModThread(i, monitor);
list.add(modThread);
}
for (ModThread a : list) {
a.start();
}
}
}
class ModThread extends Thread {
private int modValue;
private TenThreads monitor;
public ModThread(int modValue, TenThreads monitor) {
this.modValue = modValue;
this.monitor = monitor;
}
#Override
public void run() {
synchronized (monitor) {
try {
while (true) {
while (monitor.currentTaskValue % 10 != modValue) {
monitor.wait();
}
if (monitor.currentTaskValue == 101) {
break;
}
System.out.println(Thread.currentThread().getName() + " : "
+ monitor.currentTaskValue + " ,");
monitor.currentTaskValue = monitor.currentTaskValue + 1;
monitor.notifyAll();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
output
Thread-1 : 1 ,
Thread-2 : 2 ,
Thread-3 : 3 ,
Thread-4 : 4 ,
Thread-5 : 5 ,
Thread-6 : 6 ,
Thread-7 : 7 ,
Thread-8 : 8 ,
Thread-9 : 9 ,
......
.....
...
Thread-4 : 94 ,
Thread-5 : 95 ,
Thread-6 : 96 ,
Thread-7 : 97 ,
Thread-8 : 98 ,
Thread-9 : 99 ,
Thread-0 : 100 ,
Documentation are intentionally left out for you to figure it out, there are minor bugs too!
Error is thrown due to calling of wait not on proper object. wait() should be called on object on which lock is acquired, the one implied by synchronized keyword.
Well I do not have the code...but the perspective seems to be
that there are 100 tasks to be executed each of incrementing
a count by 1.
So there could be a ThreadPool of say 10 threads and these
threads are incrementing the shared count value...
Only point to consider is that the Thread pools worker threads
have to sequentially execute their tasks one after the other
and the thread sequence for the 10 have to be maintained...
One simple way to solve this is use below state in runnable class
private final int index;
private final AtomicInteger atomicInteger;
private final CyclicBarrier cyclicBarrier;
index - is responsible for conditional verification i.e., which number this thread should print.
atomicInteger - shared across all threads for current number.
Cyclic barrier - makes all threads to wait unit a every thread completes a cycle/iteration.
Code sample:
public class PrintSequence {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
final AtomicInteger atomicInteger = new AtomicInteger(1);
final CyclicBarrier cyclicBarrier = new CyclicBarrier(10, ()-> {
System.out.println("a cycle done");
});
IntStream.rangeClosed(0, 9)
.boxed()
.map(i -> new PrintSequenceTask(i, atomicInteger, cyclicBarrier))
.map(p -> executorService.submit(p))
.collect(Collectors.toList());
executorService.shutdown();
}
}
class PrintSequenceTask implements Runnable {
private final int index;
private final AtomicInteger atomicInteger;
private final CyclicBarrier cyclicBarrier;
PrintSequenceTask(int index, AtomicInteger atomicInteger, CyclicBarrier cyclicBarrier) {
this.index = index;
this.atomicInteger = atomicInteger;
this.cyclicBarrier = cyclicBarrier;
}
#Override
public void run(){
for(int i=1; i<10;i++){
while (((atomicInteger.get()-index-1)%10 != 0)){}
System.out.println(Thread.currentThread().getName()+" "+(atomicInteger.get()));
atomicInteger.getAndIncrement();
await();
}
}
public void await(){
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
public class BigSequence {
public static void main(String[] args) {
BigPrintNum p = new BigPrintNum();
int max = 20;
int no_threads = 11;
for(int i=0;i<no_threads;i++){
boolean b[] = new boolean[no_threads];
b[i] = true;
Thread t = new Thread(new BigPrint(p, max, b,no_threads));
t.start();
}
}
}
class BigPrint implements Runnable {
int num=0;
BigPrintNum p;
int max;
int no_threads;
boolean b[];
public BigPrint(BigPrintNum p,int max,boolean b[],int no_threads){
this.p = p;
this.max = max;
this.b = b;
this.no_threads = no_threads;
}
#Override
public void run() {
int n = 0;
for(int i=0;i<no_threads;i++){
if(b[i] == true){
n = i;
num = i;
}
}
while(num<=max){
p.print(num, n, no_threads);
num += no_threads;
}
}
}
class BigPrintNum {
int turn = 0;
public synchronized void print(int n,int i,int no_threads){
while(this.turn != i){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println(i + "th seq = " + n);
this.turn = (i+1)%no_threads;
notifyAll();
}
}
Its a generic one, where we can use any number of threads and use any max value.
public class ThreadSequence
{
public static int totalThread;
public static void main(String[] args)
{
MyLock myLock = new MyLock();
totalThread = 10;
for(int i=1;i<=totalThread;i++)
{
MyThread myThread = new MyThread(i,myLock);
myThread.start();
}
}
}
class MyLock
{
public int counter = 0;
}
MyThread Class
class MyThread extends Thread{
public MyLock lock;
public int no;
public MyThread(int no,MyLock lock)
{
super("My Thread No "+no);
this.no = no;
this.lock = lock;
}
public void run()
{
synchronized (lock)
{
while(true)
{
while(lock.counter%ThreadSequence.totalThread !=(this.no-1))
{
try
{
if(lock.counter > 99)
{
break;
}
lock.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
if(lock.counter > 99)
{
break;
}
System.out.println("Current Thread "+Thread.currentThread().currentThread()+" --- Current Count "+(lock.counter+1));
lock.counter = lock.counter +1 ;
lock.notifyAll();
}
}
}
}
print 1 to 100 number alternatively by each thread similar way you can print for 10 threads- m1 and m2 like
m1-1
m2-2
m3-3
m4-4
public class MultiThread extends Thread {
static volatile int num=0;
public static void main(String[] args) {
MultiThread m1= new MultiThread();
MultiThread m2= new MultiThread();
m1.setName("m1");
m1.setPriority(5);
m2.setName("m2");
m2.setPriority(5);
m1.start();
m2.start();
}
#Override
public void run() {
while(num<100) {
num +=1;
print();
}
}
private void print(){
synchronized(this) {
System.out.println(currentThread().getName()+" "+ num);
try {
currentThread().wait(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
The simple thing to do is to hold common resource for all of them.
Hold a List and every thread will insert into the list, in the end you can sort and print..
If you want them to do it on your order it won't be very effective because you won't need 10 threads to do it..
This way it will be faster and will use 10 threads to do some work, but when everyone finish you still need to do some work
public class PrintNumbersbyThreads implements Runnable {
private int i;
public PrintNumbersbyThreads(int i) {
this.i = i;
}
public static void main(String[] args) {
PrintNumbersbyThreads p = new PrintNumbersbyThreads(1);
PrintNumbersbyThreads p2 = new PrintNumbersbyThreads(2);
PrintNumbersbyThreads p3 = new PrintNumbersbyThreads(3);
Thread t1 = new Thread(p, "t1");
Thread t2 = new Thread(p2, "t2");
Thread t3 = new Thread(p3, "t3");
t1.start();
try {
t1.join();
t2.start();
t2.join();
t3.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void run() {
System.out.println("\n" + Thread.currentThread().getName() + " prints ");
for (int j = 0; j < 10; j++) {
System.out.print(i + " ");
i = i + 10;
}
}
}
Written sample code 3 Threads and the output is
t1 prints:
1 11 21 31 41 51 61 71 81 91
t2 prints:
2 12 22 32 42 52 62 72 82 92
t3 prints:
3 13 23 33 43 53 63 73 83 93
Hope this is what you are Looking for?
I have written one generic code which will take the number till where you want to print and the number of threads to be used.
public class ThreadedPrinting {
private Object locks[];
private static class Printer extends Thread {
int curVal;
int endVal;
Object myLock;
Object nextLock;
int step;
public Printer(int startFrom, int endVal, int step, Object myLock, Object nextLock){
this.curVal = startFrom;
this.endVal = endVal;
this.step = step;
this.myLock = myLock;
this.nextLock = nextLock;
this.step = step;
}
#Override
public void run(){
synchronized(myLock) {
while (curVal <= endVal) {
try {
myLock.wait();
System.out.println(curVal);
curVal += step;
}
catch(InterruptedException e) {}
synchronized(nextLock) {
nextLock.notify();
}
}
}
synchronized(nextLock) {
nextLock.notify(); /// this ensures all worker threads exiting at the end
}
}
} // Printer
public ThreadedPrinting(int maxNum, int threads) {
locks = new Object[threads];
int i;
for(i = 0; i < threads; ++i) locks[i] = new Object();
for(i = 0; i < threads -1 ; ++i) {
Printer curPrinter = new Printer(i, maxNum, threads, locks[i], locks[i+1]);
curPrinter.start();
}
Printer lastPrinter = new Printer(i, maxNum, threads, locks[threads - 1], locks[0]);
lastPrinter.start();
}
public void start() {
synchronized (locks[0]) {
locks[0].notify();
}
}
public static void main(String[] args) {
ThreadedPrinting printer = new ThreadedPrinting(1000,7);
printer.start();
}
}
The same problem can be solved by usign Phaser as well but the order is not restrictive but will be in round-robin fashion. I have provided the solution for similar problem here.
I am working on the producer-consumer problem in Java in which the producer is writing Fibonacci numbers in the pipe and the consumer is consuming it via pipe reader and checking whether it is prime or not.
The problem is that only first 3 Fibonacci primes are generated by the code given below.
What is wrong with it?
package fibonacciprime;
import java.io.*;
import java.lang.*;
public class FibonacciPrime extends Thread {
public static void main(String[] args) throws Exception {
//final PipedOutputStream pout=new PipedOutputStream();
//final PipedInputStream pin=new PipedInputStream();
final PipedWriter pwriter = new PipedWriter();
final PipedReader preader = new PipedReader(pwriter);
//pout.connect(pin);
Thread threadA=new Thread()
{
public void run()
{
for(int i=2;i<1000;i++)
{
synchronized(pwriter)
{
try
{
int temp=5*i*i-4;
int temp1=5*i*i+4;
int p=(int)Math.sqrt(temp1)*(int)Math.sqrt(temp1);
int q=(int)Math.sqrt(temp)*(int)Math.sqrt(temp);
if(p==temp1 || q==temp)
pwriter.write(i);
}catch(Exception e){e.printStackTrace();}
}
}
try {
pwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread threadB = new Thread()
{
public void run()
{
int flag=0;
try
{
int temp;
while( ( temp = preader.read() ) != -1)
{
//int k=pin.read();
for(int i=2;i*i < temp;i++)
{
if(temp%i==0)
{
flag=1;
break;
}
}
Thread.sleep(100);
if(flag==0)
System.out.println(temp);
}
preader.close();
}catch(Exception e){e.printStackTrace();}
}
};
threadA.start();
threadB.start();
}
}
Your producer thread is completing its task, but your consumer is buggy, so it doesn't print the appropriate values.
You declare your flag for detecting a prime number outside your while-loop, and never reset its value. Because of this, once the first non-prime number is read (8), all numbers after that will be treated as composite, even when they are prime.
You just need to move the declaration of flag inside your while-loop and your program will work:
while ((temp = preader.read()) != -1) {
int flag = 0; // moved this to inside the loop
for (int i = 2; i * i < temp; i++) {
if (temp % i == 0) {
flag = 1;
break;
}
}
Thread.sleep(100);
if (flag == 0) System.out.println(temp);
}
I have a problem with Threads in Java.
I would like to write a program where there is Class Main which has ArrayList of Threads of some class (Class Task) which just writes a letter and the number. Object Main just wakes one Thread from ArrayList and let it to do something while the same object(Main) sleeps another one.
But there is one problem even if I change the Main.ACTIVE to false it does not end all of the Threads some stay on, and it's random, I just would like to make them end and write:
I am saying goodbay + character - sth like that
public class Main extends Thread {
ArrayList<Thread> threads;
static boolean ACTIVE = true;
public Main() {
super();
threads = new ArrayList<Thread>();
}
public void run(){
Object monitor = new Object();
for (int i = 0; i <= 5; i++) {
threads.add(new Thread(new Task(i + 65, monitor)));
}
long cT = System.currentTimeMillis();
for (int i = 0; i < threads.size(); i++) {
threads.get(i).start();
}
System.out.println("BEFORE synchronized(monitor)");
synchronized(monitor){
while (System.currentTimeMillis() - cT < 1000) {
try{
monitor.notify();
Thread.sleep(50);
monitor.wait();
} catch(Exception e){
e.printStackTrace();}
}
System.out.println("BEFORE ACTIVE= FALSE and after WHILE in Main");
ACTIVE = false;
for(int i = 0; i < threads.size(); i++){
System.out.println(threads.get(i).getState());
}
}
System.out.println("LAST COMMAND IN MAIN");
}
}
public static void main(String[] args) {
new Main().start();
//new Thread(new Task(65)).start();
}
}
And the Task Class
public class Task implements Runnable {
int nr;
char character;
Object monitor;
public Task(int literaASCII, Object monitor) {
this.nr = 0;
this.monitor = monitor;
character = (char) (literaASCII);
}
#Override
public void run() {
synchronized (monitor) {
while (Main.ACTIVE) {
try {
System.out.println("ENTERING WHILE IN TASK");
monitor.wait();
System.out.print(nr + "" + character + ", ");
nr++;
int r = (int) ((Math.random() * 50) + 50); // <500ms,1000ms)
Thread.sleep(r);
} catch (Exception e) {e.printStackTrace();}
monitor.notify();
System.out.println("YYYYYYYYY");
}
System.out.println("AFTER WHILE IN Task");
}
System.out.println("I am saying goodbye " + character);
}
}
I would recommend that you look at the more modern concurrency classes in java.util.concurrent package, especially ExecutorService. And read "Java Concurrency In Practice."
Your problem is for starters that ACTIVE should be marked as volatile. Any variable that is shared by multiple threads needs to somehow be synchronized or marked as volatile so that it will have a memory barrier around its reading and writing.
Another thing you can do from a boolean standpoint is to use the AtomicBoolean class instead of a volatile boolean.
Instead of a static volatile boolean, you might instead consider to have a volatile boolean for each Task object so that Main has more fine grained control over the individual tasks and you are using a static "global" variable. You could even add a task.shutdown() method to set the active flag.
Lastly, as #duffmo mentioned, you should always consider using one of the thread-pools ExecutorService if you always just want to have one thread running. Something like Executors.newFixedThreadPool(1). But I can't quite tell if you only want one thread all of the time. If you used an ExecutorService then main would just do:
ExecutorService threadPool = Executors.newFixedThreadPool(1);
List<Future> futures = new ArrayList<Future>();
for (int i = 0; i <= 5; i++) {
// the monitor would not be needed
threadPool.submit(new Task(i + 65));
}
threadPool.shutdown();
for (Future future : futures) {
// this waits for the working task to finish
future.get();
}
But if you need your background task to stop and start like it is currently doing with the monitor then this model might not work.
Now naswer is
0A, 0B, 0C, 0D, 0E, 0F, 1A, 1B, 1C, 1D, 1E, 1F, WAITING
WAITING
WAITING
WAITING
WAITING
WAITING
LAST COMMAND IN MAIN
I added sleep after starting threads
import java.util.ArrayList;
public class Main extends Thread {
ArrayList<Thread> threads;
volatile static boolean ACTIVE = true;
public Main() {
super();
threads = new ArrayList<Thread>();
}
public void run(){
Object monitor = new Object();
for (int i = 0; i <= 5; i++) {
threads.add(new Thread(new Task(i + 65, monitor)));
}
long cT = System.currentTimeMillis();
for (int i = 0; i < threads.size(); i++) {
threads.get(i).start();
}
try{Thread.sleep(50);}catch(Exception e){e.printStackTrace();}
// System.out.println("BEFORE synchronized(monitor)");
synchronized(monitor){
while (System.currentTimeMillis() - cT < 1000) {
try{
monitor.notify();
Thread.sleep(500);
monitor.wait();}catch(Exception e){e.printStackTrace();}
}
// System.out.println("BEFORE ACTIVE= FALSE and after WHILE in Main");
ACTIVE = false;
for(int i = 0; i < threads.size(); i++){
System.out.println(threads.get(i).getState());
}
}
System.out.println("LAST COMMAND IN MAIN");
}
public static void main(String[] args) {
new Main().start();
//new Thread(new Task(65)).start();
}
}
and the TASK
public class Task implements Runnable {
int nr;
char character;
Object monitor;
public Task(int literaASCII, Object monitor) {
this.nr = 0;
this.monitor = monitor;
character = (char) (literaASCII);
}
#Override
public void run() {
synchronized (monitor) {
while (Main.ACTIVE) {
try {
// System.out.println("ENTERING WHILE IN TASK");
monitor.wait();
System.out.print(nr + "" + character + ", ");
nr++;
int r = (int) ((Math.random() * 50) + 50); // <500ms,1000ms)
Thread.sleep(r);
} catch (Exception e) {e.printStackTrace();}
monitor.notify();
// System.out.println("YYYYYYYYY");
}
System.out.println("AFTER WHILE IN Task");
}
System.out.println("I am saying goodbye " + character);
}
}