Unexpected output for 2 threads modifying a shared BlockingQueue - java

I have the followings:
public class ProducerConsumer {
private BlockingQueue<Integer> q;
private Random rnd;
private boolean run;
public ProducerConsumer(){
rnd = new Random();
q = new ArrayBlockingQueue<>(10);
run = true;
}
// high leve - with ArrayBlockingQueue
public void QProducer() throws InterruptedException{
int i;
while(run){
i = rnd.nextInt(100);
q.put(i);
System.out.println(i +" Added. Size is: "+ q.size());
}
}
public void QConsumer() throws InterruptedException{
int i;
while(run){
Thread.sleep(100);
if (rnd.nextInt(10) == 0) {
i = q.take();
System.out.println(i + " Taken. Size is: "+ q.size());
}
}
}
public void changeRun(){
run = false;
}
}
public static void main(String[] args) throws InterruptedException {
// Producer Consumer
final ProducerConsumer pc = new ProducerConsumer();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
try {
pc.QProducer();
} catch (InterruptedException ex) {
Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Thread t2 = new Thread(new Runnable() {
#Override
public void run() {
try {
pc.QConsumer();
} catch (InterruptedException ex) {
Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t1.start();
t2.start();
Scanner scn = new Scanner(System.in);
scn.nextLine();
pc.changeRun();
t1.join();
t2.join();
}
Ouptput:
20 Added. Size is: 1
8 Added. Size is: 2
71 Added. Size is: 3
72 Added. Size is: 4
61 Added. Size is: 5
97 Added. Size is: 6
6 Added. Size is: 7
64 Added. Size is: 8
58 Added. Size is: 9
27 Added. Size is: 10
20 Taken. Size is: 10 *
93 Added. Size is: 10
8 Taken. Size is: 9
95 Added. Size is: 10
71 Taken. Size is: 10 *
70 Added. Size is: 10
72 Taken. Size is: 10 *
85 Added. Size is: 10
61 Taken. Size is: 9
43 Added. Size is: 10
64 Added. Size is: 10 **
...
I wonder how can there be numbers that are taken, and yet the size remains the same ( * ),
and howcome values are added after Queue is full ( ** ).
AFAIU, BlockingQueue is sychronized, and waits for values to to be added if queue is empty, and to be removed if full.
Thank you in advance.

The put() and the println/size() after it, and the take() and the println/size() after it, can be interleaved because of the multithreading. So the value returned by size() may be out-of-date by the time it is printed. E.g.
1a. Add (size = 10)
1b. Print size 10
2a. Take (size = 9)
3a. Add (size = 10)
2b. Print size 10
3b. Print size 10

Since you have no synchronized blocks, your put operations are not atomic with size queries. Between those two any number of actions by other threads can occur—and has occurred, as you witness.

Related

Java multithreading query

Hi I am a newbie in concurrent programming domain, I was testing the below code and seems the while loop is not terminating for threads. Could someone help explain whats happening here.
public class PrimePrinter{
public long counter = 0;
public synchronized long getCounter() {
return counter++;
}
public Thread makeThread() {
Runnable rn = new Runnable() {
/* (non-Javadoc)
* #see java.lang.Runnable#run()
*/
#Override
public void run() {
while (counter < 100) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " : " +getCounter());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
return new Thread(rn);
}
public static void main(String[] args) {
int n = 10;
Thread[] threads = new Thread[10];
PrimePrinter pp = new PrimePrinter();
for(int i = 1; i < n; i++) {
threads[i] = pp.makeThread();
threads[i].start();
}
}
}
Last few lines of output
Thread-4 : 81
Thread-5 : 87
Thread-7 : 91
Thread-5 : 97
Thread-2 : 95
Thread-4 : 98
Thread-6 : 96
Thread-8 : 90
Thread-1 : 93
Thread-3 : 92
Thread-0 : 94
Thread-2 : 99
Thread-6 : 107
Thread-3 : 103
Thread-0 : 104
Thread-1 : 105
Thread-8 : 106
Thread-5 : 102
Thread-4 : 101
Thread-7 : 100
This is not working for one reason that is crucial not to miss. Consider the way your code actually runs. It checks the counter value, then sleeps, THEN prints THEN increments.
You have 9 threads running at once. Then, 8 of these threads check the value, and at the time, the value is less than 100. So they pass the test and keep going. All 8 then sleep for 1000ms. Meanwhile, your other thread has just incremented the value... and 8 more are about to come out of their sleep.
So, if your value is 99 (which it inevitably will be), you'll now get a value of 100 from the thread that just incremented. Then, those 8 that already passed the test are also going to be incrementing, giving the counter a resulting value of 108.
However, the very last thread to run will show a value of 107 because you print THEN increment for all runs.
If you did this the other way around (like in the code below), you would not run into this problem.
The following code works correctly:
public class PrimePrinter {
public long counter = 0;
public synchronized long getCounter() {
return counter;
}
public synchronized void incrementCounter() {
counter++;
}
public Thread makeThread() {
Runnable rn = new Runnable() {
/*
* (non-Javadoc)
*
* #see java.lang.Runnable#run()
*/
#Override
public void run() {
while (getCounter() < 100) {
try {
incrementCounter();
System.out.println(Thread.currentThread().getName()
+ " : " + getCounter());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
return new Thread(rn);
}
public static void main(String[] args) {
int n = 10;
Thread[] threads = new Thread[10];
PrimePrinter pp = new PrimePrinter();
for (int i = 1; i < n; i++) {
threads[i] = pp.makeThread();
threads[i].start();
}
}
}
Each thread waits a second after having tested that counter < 100, during this second, other threads can increment the counter.
UPDATE: You could do something like this:
while (true) {
long current = getCounter();
if (current >= 100) {
break;
}
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " : " + current);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Finding prime number using BlockingQueue in Java with multi threading

I've implemented a multi thread program using BlockingQueue to test for a number is prime or not.
The requirement is the program will prompt user to input a number and then the program will print every prime number from 2 to inputted number from user in ascending order.
I have 3 class: NumberEnumerationTask for initialize BlockingQueue contains all numbers to be check, PrimeRunner for check the number is prime or not, PrimeChecker for the main class.
NumberEnumerationTask.java
package multithread;
import java.util.concurrent.BlockingQueue;
public class NumberEnumerationTask implements Runnable {
private BlockingQueue<Integer> queue;
private Integer maximum;
public static Integer DUMMY = new Integer(0);
public NumberEnumerationTask(BlockingQueue<Integer> queue, Integer maximum) {
this.queue = queue;
this.maximum = maximum;
}
#Override
public void run() {
try {
enumerate();
queue.put(DUMMY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Create a BlockingQueue contain Integer number from 1 to maximum.
* #throws InterruptedException
*/
private void enumerate() throws InterruptedException {
for (int i = 2; i < maximum; i++) {
queue.put(i);
}
}
}
PrimeRunner.java
package multithread;
import java.util.concurrent.BlockingQueue;
public class PrimeRunner implements Runnable {
private BlockingQueue<Integer> queue;
public PrimeRunner(BlockingQueue<Integer> queue) {
this.queue = queue;
}
#Override
public void run() {
try {
boolean done = false;
while (!done) {
Integer checkNumber = queue.take();
if (checkNumber == NumberEnumerationTask.DUMMY) {
queue.put(checkNumber);
done = true;
} else {
checkPrimeNumber(checkNumber);
}
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
private void checkPrimeNumber(Integer number) {
boolean isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
queue.remove(number);
break;
}
}
if (isPrime == true) {
System.out.print(number + " ");
queue.remove(number);
}
}
}
PrimeChecker.java
public class PrimeChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter maximum number to check: ");
Integer number = sc.nextInt();
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(number);
NumberEnumerationTask initialize = new NumberEnumerationTask(queue, number);
new Thread(initialize).start();
for (int i = 0; i <= number; i++) {
new Thread(new PrimeRunner(queue)).start();
}
sc.close();
}
}
The DUMMY variable is to signal completion.
When I run the program, sometime it's not print in ascending order, sometime it does.
Enter maximum number to check: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Enter maximum number to check: 100
2 3 5 7 11 13 17 19 23 29 31 41 43 47 37 59 61 53 67 71 73 79 83 89 97
Can someone tell me what is wrong with my code? Thanks
In PrimeChecker, the following code is the cause:
for (int i = 0; i <= number; i++) {
new Thread(new PrimeRunner(queue)).start();
}
You create multiple PrimeRunner instances and a thread for each of them, so something like below can happen:
PrimeRunner in thread 1 takes 5.
PrimeRunner in thread 2 takes 7.
PrimeRunner in thread 2 prints 7. (PrimeRunner in thread 1 is still checking...)
PrimeRunner in thread 1 prints 5.
If you run a single PrimeRunner you can avoid such cases and it works as you expected.
If you still want to run multiple PrimeRunner surrounding the following part by synchronized block with an object in a static field of PrimeRunner should solve the problem:
Integer checkNumber = queue.take();
if (checkNumber == NumberEnumerationTask.DUMMY) {
queue.put(checkNumber);
done = true;
} else {
checkPrimeNumber(checkNumber);
}
Thread safety is the major issue. In your code, queue is shared among threads which is being processed without any synchronization mechanism hence data corruption likely to happen hence unpredictable result. If you run it multiple times you might get different results.
Multi-threading could be used for prime check to speed up the main goal i.e. prime finding which could be achieved by splitting the big number N into multiple parts and individual thread would work upon these parts and one thread to aggregate the results.

Print timed thread

I'm working on the following assignment:
Consider a shared counter whose values are non-negative integers,
initially zero. A time-printing thread increments the counter by one
and prints its value each second from the start of execution. A
message-printing thread prints a message every fifteen seconds. Have
the message-printing thread be notified by the time-printing thread as
each second passes by. Add another message-printing thread that prints
a different message every seven seconds. Such addition must be done
without modifying the time-printing thread implementation.
Have all involved threads share the counter object that is updated by
the time-printing thread every second. The time-printing thread will
notify other threads to read the counter object each time it updates
the counter, then each message-printing thread will read the counter
value and see if its assigned time period has elapsed; if so, it will
print its message.
import java.lang.Class;
import java.lang.Object;
public class Main2 {
public static void main(String... args)
{
Thread thread = new Thread()
{
public void run()
{
int x = 0;
while(true)
{
x = x + 1;
System.out.print(x + " ");
if(x%7 == 0)
{
System.out.println();
System.out.println("7 second message");
}
if(x%15 == 0)
{
System.out.println();
System.out.println("15 second message");
}
try { Thread.sleep(1000); }
catch (Exception e) { e.printStackTrace(); }
}
}
};
thread.start();
}
}
This outputs what I want it to, but the requirement calls for multiple threads to output when the 7 and 15 second messages show. I can't wrap my head around how to use multiple threads to do this.
You have to remove the ";" after if conditions.
if(x%7 == 0);
and
if(x%15 == 0);
Check the following code
public static void main(String... args) {
Thread thread = new Thread() {
public void run() {
int x = 0;
while (true) {
x = x + 1;
System.out.print(x + " ");
if (x % 7 == 0)
{
System.out.println();
System.out.println("7 second message");
}
if (x % 15 == 0)
{
System.out.println();
System.out.println("15 second message");
}
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
My output for this as follows
1 2 3 4 5 6 7
7 second message
8 9 10 11 12 13 14
7 second message
15
15 second message
16 17 18 19 20 21
7 second message
22 23 24 25 26 27 ...

Print 1 to 100 using 10 threads in java

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.

Why thread with lower priority get longer CPU time than thread with higher priority in this code

I'm reading Threads from Java The complete Reference - Herbert Schildt (TATA McGRAW HILL). And in this example form book, Thread with lower priority should give lower value of clicks than Thread with higher priority. But what i got
low-priority thread 3000255895
hi-priority thread 2857361716
according to book it is something like this (difference)
low-priority thread 4408112
hi-priority thread 589626904
code
class Clicker implements Runnable {
long click = 0;
Thread t;
private volatile boolean running = true;
public Clicker (int p) {
t = new Thread(this);
t.setPriority(p);
}
public void run() {
while(running) {
click++;
}
}
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
class Priority {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker hi = new Clicker(Thread.NORM_PRIORITY + 2);
Clicker lo = new Clicker(Thread.NORM_PRIORITY - 2);
hi.start();
lo.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("main thread interrupted");
}
lo.stop();
hi.stop();
try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {
System.out.println("interrupted exception catched in main");
}
System.out.println("low-priority thread " + lo.click);
System.out.println("hi-priority thread " + hi.click);
}
}
I'm on Intel® Core™ i3-2310M CPU # 2.10GHz × 4 with Ubuntu 13.04
with Thread.sleep(50000);
low-priority thread 14849893875
hi-priority thread 14224080358
Is it not a big difference
I also try with priority HI = 10 and LOw = 1, But result nearly same.
UPDATE:
After running with 100 threads 50 with priority = 1 and 50 with priority = 10 result is
here time = clicks values
high = 82 time = 110117529
low = 83 time = 102549208
high = 84 time = 110905795
low = 85 time = 99100530
high = 86 time = 105012756
low = 87 time = 110195297
high = 88 time = 102820088
low = 89 time = 97814606
high = 90 time = 99990839
low = 91 time = 102326356
high = 92 time = 98656119
low = 93 time = 98127243
high = 94 time = 97097823
low = 95 time = 103604394
high = 96 time = 93632744
low = 97 time = 99032550
high = 98 time = 103879116
low = 99 time = 97179029
With 1000 Threads Take 4-5 min. to complete, all 4 cores at 100%
low = 977 time = 23593983
high = 978 time = 23998970
low = 979 time = 23879458
high = 980 time = 22775297
low = 981 time = 21297504
high = 982 time = 22464600
low = 983 time = 20301501
high = 984 time = 19073992
low = 985 time = 19450707
high = 986 time = 19317769
low = 987 time = 18454648
high = 988 time = 17901939
low = 989 time = 16976661
high = 990 time = 17360728
low = 991 time = 16813824
high = 992 time = 15531501
low = 993 time = 14659965
high = 994 time = 12833364
low = 995 time = 13708670
high = 996 time = 13348568
low = 997 time = 12947993
high = 998 time = 12749436
low = 999 time = 9804643
Unpredictable output for each thread.
I suspect the example figures will be somewhat dependent upon both your platform and the author's (does the example run upon a single core?). The JVM thread prioritisation is OS/platform-specific. From this interesting document
Linux priorities
Under Linux, you have to go through more hoops to get thread
priorities to function at all, although in the end, they may be more
useful than under Windows. In Linux:
thread priorities only work as of Java 6 onwards;
for them to work, you must be running as root (or with root privileges via setuid);
the JVM parameter -XX:UseThreadPriorities must be included
So check your Java version, your Java invocation and your privileges. I suspect (!) you only satisfy one of those criteria (your Java/JVM version).
Apart from Thread priorities being a gray area under most implementations and platforms, other things you could look into are :
In all probability, click is not going to contain the exact number of invocations. This should have been an AtomicLong. So how large do you want the difference to be ? +-2 isn't a big difference either.
Run one thread with MIN_PRIORITY and other thread with MAX_PRIORITY. Calculate the difference. Repeat the above experiment with 10, 100, 1000 threads. 50% of them with MIN_PRIORITY and other 50% with MAX_PRIORITY. Your results if you post will be surely enlightening
Edit
Interesting, let me submit my sample program. I am under OpenJDK6 and the thread priorities :MIN_PRIORITY and MAX_PRIORITY doesn't seem to matter much. The number of clicks with the high level thread is slightly more than the number of clicks with low priority thread.
import java.util.concurrent.atomic.AtomicLong;
class Clicker implements Runnable {
AtomicLong click = new AtomicLong();
Thread t;
private volatile boolean running = true;
public Clicker (int p) {
t = new Thread(this);
t.setPriority(p);
}
public void run() {
while(running) {
click.incrementAndGet();
longOP();
}
}
private void longOP() {
outer:
for (int i = 2; i <= 10000 && running; i++) {
final int NUM = i;
for (int j = 2; j * j <= NUM && running; j++) {
if (NUM % j == 0) {
continue outer;
}
}
System.out.print("[" + NUM + "],");
}
System.out.println();
for (int i = -90; i <= 90 && running; i++) {
double rad = Math.toRadians(i);
double value = Math.sqrt(Math.abs(Math.pow(Math.PI * Math.sin(rad), 10.0))) + Math.tan(rad) * Math.cos(rad); //lol
System.out.print("[" + i + " " + value + "],");
}
}
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
public class Priority {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
final int MAX_THREADS = 1000;
final long MAX_RUN_TIME = 60 * 1000; //millis
Clicker clickHi[] = new Clicker[MAX_THREADS];
Clicker clickLow[] = new Clicker[MAX_THREADS];
for (int i = 0; i < MAX_THREADS; ++i) {
clickHi[i] = new Clicker(Thread.MAX_PRIORITY);
clickLow[i] = new Clicker(Thread.MIN_PRIORITY);
clickHi[i].start();
clickLow[i].start();
}
try {
Thread.sleep(MAX_RUN_TIME);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("main thread interrupted");
}
for (int i = 0; i < MAX_THREADS; ++i) {
clickHi[i].stop();
clickLow[i].stop();
}
for (int i = 0; i < MAX_THREADS; ++i) {
try {
clickHi[i].t.join();
clickLow[i].t.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("interrupted exception catched in main");
}
}
long totalLowClicks = 0, totalHighClicks = 0;
for (int i = 0; i < MAX_THREADS; ++i) {
totalLowClicks += clickLow[i].click.longValue();
totalHighClicks += clickHi[i].click.longValue();
System.out.println("low-priority thread " + clickLow[i].click);
System.out.println("hi-priority thread " + clickHi[i].click);
}
System.out.println("Sum of LOW clicks : " + totalLowClicks);
System.out.println("Sum of HI clicks : " + totalHighClicks);
}
}
The above program is cpu intensive. You may encounter Out Of Memory Errors. But just for fun, post your result.

Categories