I have just started learning threads and pretty new to it.I'm trying to print alphabets and numbers one after the other.I have synchronized them using a flag but of no use.
public class Alphabets {
public static void main(String[] args) {
AN an= new AN(false);
Thread t1=new Thread(new Runnable() {
#Override
public void run() {
try {
an.Alpha();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread t2= new Thread(new Runnable() {
#Override
public void run() {
try {
an.numbers();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
...
class AN
{
boolean flag;
AN(boolean flag)
{
this.flag=flag;
}
synchronized void Alpha() throws InterruptedException
{
if(flag==false)
{
for(char i='A'; i<='Z';i++)
{
System.out.println(+i);
notifyAll();
flag=true;
}
}
else
{
wait();
}
}
synchronized void numbers() throws InterruptedException
{
if(flag==true)
{
for(int i=1;i<=26;i++)
{
System.out.println(+i);
notifyAll();
flag=false;
}
}
else
{
wait();
}
}
}
My desired output is : a1b2c3d4....
My console output is : abcd...1234...
Can anybody point out the mistake since I'm unable to synchronize these two threads.
Change class AN to check the flag in while loop.
public class AN {
boolean flag;
AN(boolean flag) {
this.flag = flag;
}
synchronized void Alpha() throws InterruptedException {
for(char i = 'A'; i <= 'Z'; i++) {
while(flag == true) {
wait();
}
System.out.println(i);
notifyAll();
flag = true;
}
}
synchronized void numbers() throws InterruptedException {
for(int i = 1; i <= 26; i++) {
while(flag == false) {
wait();
}
System.out.println(i);
notifyAll();
flag = false;
}
}
Well, what you want is more or less pipelining, therefore the threads need to know when they are allowed to work.
I'd therefore use a Queue to await the input on one thread and wait for it to be set in the other thread.
Class AN:
import java.util.concurrent.BlockingQueue;
class AN
{
BlockingQueue<Boolean> input;
BlockingQueue<Boolean> output;
AN(BlockingQueue<Boolean> input, BlockingQueue<Boolean> output)
{
this.input = input;
this.output = output;
}
void Alpha() throws InterruptedException
{
for (char i = 'a'; i <= 'z'; i++) {
input.take();
System.out.print(i);
output.put(Boolean.TRUE);
}
}
void numbers() throws InterruptedException
{
for (int i = 1; i <= 26; i++) {
input.take();
System.out.print(i);
output.put(Boolean.TRUE);
}
}
}
Class Test (or where your main is):
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Test
{
public static void main(String[] args) throws InterruptedException
{
BlockingQueue<Boolean> input = new LinkedBlockingQueue<>();
BlockingQueue<Boolean> output = new LinkedBlockingQueue<>();
AN an1 = new AN(output, input);
AN an2 = new AN(input, output);
output.add(Boolean.TRUE);
Thread t1 = new Thread(new Runnable()
{
#Override
public void run()
{
try {
an1.Alpha();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable()
{
#Override
public void run()
{
try {
an2.numbers();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
Outputs: a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20u21v22w23x24y25z26
You can achieve this by using BlockingQueue and Object's wait and notify methods.
public class AlphaNum {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
AtomicBoolean flag = new AtomicBoolean(Boolean.TRUE);
Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
try {
for(int i=1;i<=26;i++){
synchronized (lock){
while (flag.get()){
lock.wait();
}
System.out.print(i);
flag.set(Boolean.TRUE);
lock.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
#Override
public void run() {
try {
for(char c='A';c<='Z';c++){
synchronized (lock){
while (!flag.get()){
lock.wait();
}
System.out.print(c);
flag.set(Boolean.FALSE);
lock.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
The above code prints a1b2c3d4.......z26
There are many ways to achieve this, Below one is one of them.
package interview;
public class TestAplhaAndNumberPrinterThreads {
// created object as static so we can access print method of this class from thread's run methods
public static TestAplhaAndNumberPrinterThreads output = new TestAplhaAndNumberPrinterThreads();
private final Object syncer = new Object();
private int state = 0;
public void print(char character) throws InterruptedException {
synchronized (syncer) {
while (true) {
if (state == 0) {
System.out.print(character + ", ");
state = 1;
syncer.notify();
return;
} else {
syncer.wait();
}
}
}
}
public void print(int number) throws InterruptedException {
synchronized (syncer) {
while (true) {
if (state == 1) {
System.out.print(number + ", ");
state = 0;
syncer.notify();
return;
} else {
syncer.wait();
}
}
}
}
public static void main(String[] args) {
NumberPrinter numberPrinter = new NumberPrinter();
AlphabetsPrinter alphabetsPrinter = new AlphabetsPrinter();
numberPrinter.start();
alphabetsPrinter.start();
}
}
class NumberPrinter extends Thread {
#Override
public void run() {
try {
for (int i = 1; i <= 26; i++) {
TestAplhaAndNumberPrinterThreads.output.print(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AlphabetsPrinter extends Thread {
#Override
public void run() {
try {
for (char i = 'a'; i <= 'z'; i++) {
TestAplhaAndNumberPrinterThreads.output.print(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// output: a, 1, b, 2, c, 3, d, 4, e, 5, f, 6, g, 7, h, 8, i, 9, j, 10, k, 11, l, 12, m, 13, n, 14, o, 15, p, 16, q, 17, r, 18, s, 19, t, 20, u, 21, v, 22, w, 23, x, 24, y, 25, z, 26,
I think the big things is for you to know, how to use break points to debug in IDE and why your code was not working. You can see the process of your code will run. I have post the picture at the last and this code which will work like your wish
class AN {
boolean flag;
AN(boolean flag) {
this.flag = flag;
}
synchronized void Alpha() throws InterruptedException {
if (flag == false) {
for (char i = 'A'; i <= 'Z'; i++) {
System.out.println(i);
flag = true;
notify();
wait();
}
}
}
synchronized void numbers() throws InterruptedException {
if (flag == true) {
for (int i = 1; i <= 26; i++) {
System.out.println(+i);
flag = false;
notify();
wait();
}
}
}
}
enter image description here
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Im trying to understand how implement Dining Savages using monitors. I have three classes, and im using the kitchen class to act as the monitor for when the pot is empty or not.
For some reason i keep getting a null pointer exception at thread two in my example below.
class Kitchen {
Kitchen k;
int c;
boolean empty;
Cook chef;
Kitchen() {
this.c = 0;
this.empty = true;
chef = new Cook(k);
}
synchronized void putServingsInPot(int servings) {
if (empty) {
this.c = servings;
}
empty = false;
notify();
}
synchronized void getServingsFromPot() {
while (empty) {
try {
System.out.println("Bout to wait");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("In Catch");
e.printStackTrace();
}if (c == 0) {
empty = true;
chef.run();
}else if(c > 0 && empty == false){
c--;
}
}
}
}
class Savage extends Thread {
Kitchen k;
Savage(Kitchen k) {
this.k = k;
}
public void run() {
while (true) {
k.getServingsFromPot();
try {Thread.sleep(500); // eat
} catch (Exception e) { return;}
}
}
}
class Cook extends Thread {
Kitchen k;
Cook(Kitchen k) {
this.k = k;
}
public void run() {
while (true) {
try {Thread.sleep(500); // sleep
} catch (Exception e) {return;}
k.putServingsInPot(10); // 10 servings
}
}
}
public class main {
public static void main(String Args[]) {
// Kitchen testing
Kitchen k = new Kitchen();
Cook c = new Cook(k);
c.start();
Savage sc[] = new Savage[9];
for (int i = 0; i < 9; i++) {
sc[i] = new Savage(k);
sc[i].start();
}
try {
Thread.sleep(5000);
} catch (Exception e) {
}
for (int i = 0; i < 9; i++) {
sc[i].interrupt();
}
c.interrupt();
System.out.println("Done\n");
}
}
Is it possible to synchronized these events without using a semaphore within the monitor ?
Look at the definition of Kitchen:
class Kitchen {
Kitchen k; // Here the kitchen is null
int c;
boolean empty;
Cook chef;
Kitchen() {
this.c = 0;
this.empty = true;
chef = new Cook(k); // here you give a null object to the cook constructor
}
you are giving a null object to the Cook constructor. Maybe you want to give yourself to the Cook object:
class Kitchen {
//Kitchen k; I don't think you will need it anymore, you can delete this line
int c;
boolean empty;
Cook chef;
Kitchen() {
this.c = 0;
this.empty = true;
chef = new Cook(this); // give yourself to the Cook
}
I want to print a series of 1 to 100 number using n number of threads (lets take 10 threads for this). Condition is 1st thread will have a sequence number from 1, 11,21....91, 2nd thread will have a sequence 2,12,22.....92 and so on. All other threads will have a sequence number like that. Now I want to print number in sequence 1 to 100. I know we can use synchronization, wait and notify method and using a variable or flag counter but I don't think this is a good idea to use it. I want to use without concurrency (like executors etc) how will I do 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) {
for (int i = 1; i <= 20; i += 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) {
for (int i = 2; i <= 20; i += 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) {
for (int i = 3; i <= 20; i += 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 to have values sorted on each threads. Each time a thread writes a number, it triggers an event in an event bus. All threads are subscribed to the event.
You start the system by triggering the event [minimum value - 1].
Each thread will receive a notification that the value [minimum value - 1] has been published. Only the thread that has the value [minimum value] will act and will trigger a new event for value [minimum value + 1].
Edit: I haven't tested it, but something like this.
static void main(String[] args) {
List<Deque<Integer>> publishQueues = new ArrayList<>();
for (int i = 1; i <= 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<>();
for (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);
for (Dequeu<Integer> publishQueue : publishQueues) {
publishQueue.addLast(integer);
}
}
}
}
}
I have program and I have set some debug point in it,but when control reached to the wait method in side the printEven method, control goes to till wait method and become hidden and nothing happened. Can any one explain how to debug wait. Control goes to wait method and never return.I am using eclipse to debug it.
public class PrintEvenOddTester
{
public static void main(String ... args)
{
Printer print = new Printer();
Thread t1 = new Thread(new TaskEvenOdd(print, 10, false));
Thread t2 = new Thread(new TaskEvenOdd(print, 10, true));
t1.start();
t2.start();
}
}
class TaskEvenOdd implements Runnable
{
private int max;
private Printer print;
private boolean isEvenNumber;
TaskEvenOdd(Printer print, int max, boolean isEvenNumber)
{
this.print = print;
this.max = max;
this.isEvenNumber = isEvenNumber;
}
public void run()
{
//System.out.println("Run method");
int number = isEvenNumber == true ? 2 : 1;
while(number<= max)
{
if(isEvenNumber)
{
//System.out.println("Even :"+ Thread.currentThread().getName());
print.printEven(number);
// number+=2;
}
else
{
//System.out.println("Odd :"+ Thread.currentThread().getName());
print.printOdd(number);
// number+=2;
}
number+=2;
}
}
}
class Printer
{
boolean isOdd= false;
synchronized void printEven(int number)
{
while(isOdd == false)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Even:"+number);
isOdd = false;
notifyAll();
}
synchronized void printOdd(int number)
{
while(isOdd == true)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Odd:"+number);
isOdd = true;
notifyAll();
}
}
My homework was to create project using parallelization that all should be proper.
However, I made my project but my profesor mentioned something is wrong in my code "please look at array list, something is not ok, maybe synchronization?".
I would like ask you community to help me and point what could be wrong. I think it might be problem with not covering by synchronize brackets my array list, am I right?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* My project finds all dividors for specific number
*It must use threads, so I made them. First I start them (first loop)
*then join them (second loop). My project must have that loops.
*Problem might be with not synchronizing methods array list...
*/
public class Main {
private final static int NUMBER = 100;
private final static List<Integer> dividors = new ArrayList<Integer>();
public static void main(String[] args) {
new Main().doStuff();
}
private int sqr;
private int sqrp1;
private void doStuff() {
sqr = (int) Math.sqrt(NUMBER);
sqrp1 = sqr + 1;
Thread[] t = new Thread[sqrp1];
//starting tasks
for (int i = 1; i < sqrp1; i++) {
final int it = i;
if (NUMBER % i == 0) {
final int e = i;
t[i] = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("sta"+e);
if (!checkContains(e)) {
addElement(e);
}
final int dividednumber = NUMBER / e;
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
}
});
t[i].start();
}
}
//calling join for tasks
for (int i = 1; i < sqrp1; i++) {
final int it = i;
if (NUMBER % i == 0) {
try {
System.out.println("sto"+i);
t[i].join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("xxx");
Collections.sort(dividors);
Integer[] arrayDividors = dividors.toArray(new Integer[0]);
for (int i = 0; i < arrayDividors.length; i++) {
System.out.println(arrayDividors[i]);
}
}
private synchronized void addElement(int element) {
dividors.add(element);
}
private synchronized boolean checkContains(int element) {
return dividors.contains(element);
}
}
Am I right changing this part, is it ok now?
t[i] = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("waiting " + e);
synchronized (this) {
System.out.println("entering " + e);
if (!checkContains(e)) {
addElement(e);
}
final int dividednumber = NUMBER / e;
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
System.out.println("leaving " + e);
}
}
});
You need to turn this into a single atomic operation.
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
Imagine you have two threads.
T1: if (!checkContains(dividednumber)) { // false
T2: if (!checkContains(dividednumber)) { // false
T1: addElement(dividednumber); // adds number
T2: addElement(dividednumber); // adds same number
If you have one addElementWithoutDuplicates, this won't happen.