Write out a letter every interval every ms multithreading java - java

I'm currently working on a problem where I have to:
Write out a letter, x amount of times, after x amount of ms. Use 4 multithreads, 3 of them start right away 1 of them starts when one of the 3 is finished.
For example: A, 10, 100, has to write out A ever 10 times every 100 miliseconds.
Im currently stuck on syncing the multithreads for them to work together at adding one sum rather than them working seporatley. Could you advise how to sync it together for it to write out the above?
Here is my code:
public class PrinterThread extends Thread {
private String letter;
private int internal;
private int amount;
public PrinterThread() {
for (int i = 1; i <= internal; i++) {
System.out.println(letter);
}
synchronized (this){
internal++;
}
try {
Thread.sleep(amount);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
PrinterThread printerThread = new PrinterThread();
PrinterThread printerThread1 = new PrinterThread();
PrinterThread printerThread2 = new PrinterThread();
PrinterThread printerThread3 = new PrinterThread();
printerThread.run();
printerThread1.run();
printerThread2.run();
printerThread3.run();
}
}

Use a BlockingQueue for synchronisation, but you do need to join with the threads from your main method otherwise your main will exit the JVM before the threads finish (or possibly even before they start).
public class PrinterThread implements Runnable {
private String letter;
private int copies;
private int amount;
public PrinterThread(String letter, int copies, int amount) {
this.letter = letter;
this.copies = copies;
this.amount = amount;
}
public void run() {
for (int i = 0; i < copies; i++) {
System.out.println(letter.repeat(copies));
try {
Thread.sleep(amount);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
QUEUE.offer(0);
}
}
public class Main {
private static BlockingQueue<Integer> QUEUE = new ArrayBlockingQueue<>(4); // at least as large as the number of threads
public static void main(String[] args) {
Thread printerThread1 = new Thread(new PrinterThread("A", 10, 100));
Thread printerThread2 = new Thread(new PrinterThread("B", 20, 50));
// etc
printerThread1.start();
printerThread2.start();
// etc
QUEUE.take(); // blocking call
new Thread(new PrinterThread("D", 30, 80)).start();
// wait for threads to finish
printerThread1.join();
printerThread2.join();
// etc
}
}
Disclaimer: This answer was thumbed in via my phone, so it may not work correctly or even compile, but there’s a good chance it will work.

Write out a letter, x amount of times, after x amount of ms. Use 4 multithreads, 3 of them start right away 1 of them starts when one of the 3 is finished.
You obviously need to create a PrinterThread constructor which takes the letter, the amount of times, and amount of millis.
I'm currently stuck on syncing the multithreads for them to work together at adding one sum rather than them working separately.
I'm not sure about the sum. If you are asking how you can start the 3rd thread then there are a number of different ways to do this. I would lock on a lock object and pass in a boolean in the constructor about whether or not the thread should wait() on the lock. As each of the other threads finish they would call notify() on the lock.
private static final Object lock = new Object();
...
public class PrinterThread {
public PrinterThread(char letter, int times, int millis, boolean waitForOthers) {
this.letter = letter;
this.times = times;
this.millis = millis;
if (waitForOthers) {
synchronized (lock) {
// wait for one of the others to notify us
lock.wait();
}
}
}
public void run() {
...
synchronized (lock) {
// notify the lock in case another thread is waiting
lock.notify();
}
}
Then start 3 PrinterThreads with a value of false and 1 of them with a value of true so that it waits.

Related

How to increase volatile int in java using multiple thread?

I have a volatile int variable with value 0 that I want to increase up to 100 using 5 thread. I am trying to generate result from 0 to 100 with no duplicate. Can anyone please help me to resolve this.
I try this approach. Is it proper?
public class Producer implements Runnable {
VolatileIncrement vo = null;
String str = null;
Producer(VolatileIncrement vo, String str){
this.vo = vo;
this.str = str;
}
#Override
public void run() {
while(vo.i < 100){
System.out.println(str+vo.increaseI());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class VolatileIncrement {
volatile Integer i = 0;
public synchronized int increaseI() {
i++;
return i;
}
}
}
public class ProducerMain {
public static void main(String[] args) {
VolatileIncrement vo = new VolatileIncrement();
Producer p1 = new Producer(vo,"I am thread 1 - ");
new Thread(p1).start();
Producer p2 = new Producer(vo,"I am thread 2 - ");
new Thread(p2).start();
Producer p3 = new Producer(vo,"I am thread 3 - ");
new Thread(p3).start();
Producer p4 = new Producer(vo,"I am thread 4 - ");
new Thread(p4).start();
Producer p5 = new Producer(vo,"I am thread 5 - ");
new Thread(p5).start();
}
}
You can use atomic1 classes for the update an integer by multi-threads
AtomicLong counter = new AtomicLong(0);
counter.getAndIncrement();
It is lock-free and thread-safe on single variable.
int counter=0;
public static synchronized void increase()
{
counter++;
}
Call this method.. Since its declared synchronized only one thread will act at a time (i.e. increment at a time).
a) there is no reason to use volatile if all accesses to that variable is within a synchronized block. Synchronized has a larger scope than volatile for syncing memory between threads.
b) you cannot make a simple i++ atomic; you need synchronization, or reentrant locks, or the said AtomicInteger.

Why am I getting different results by 2 Threads even that my method is synchronized?

This is the code I am running:
public class MyRunnableClass implements Runnable {
static int x = 30;
int y = 0;
#Override
public void run() {
for(int i=0;i<30;i++){
getFromStash();
}
}
public synchronized void getFromStash(){
x--;
y++;
}
}
and my Test class:
public class MyRunnableClassTest {
public static void main(String[] args){
MyRunnableClass aa = new MyRunnableClass();
MyRunnableClass bb = new MyRunnableClass();
Thread a = new Thread(aa);
Thread b = new Thread(bb);
a.start();
b.start();
System.out.println(aa.y);
System.out.println(bb.y);
}
}
Sometimes I see output:
30
30
and sometimes I see:
30
0
Why? The method I have, is synchronized?
I actually expect to see something like 15 - 15 but it is definetly not what I am getting.
You need to wait for the threads to finish.
a.start();
b.start();
a.join();
b.join();
System.out.println(aa.y);
System.out.println(bb.y);
At that point you should see predictable results.
Added
Now you've had a chance to play - here's my attempt at what you seem to be trying to do.
public class MyRunnableClass implements Runnable {
static AtomicInteger stash = new AtomicInteger(1000);
int y = 0;
#Override
public void run() {
try {
while (getFromStash()) {
// Sleep a little 'cause I'm on a single-core machine.
Thread.sleep(0);
// Count how much of the stash I got.
y += 1;
}
} catch (InterruptedException ex) {
System.out.println("Interrupted!");
}
}
public boolean getFromStash() {
// It must be > 0
int was = stash.get();
while (was > 0) {
// Step down one.
if (stash.compareAndSet(was, was - 1)) {
// We stepped it down.
return true;
}
// Get again - we crossed with another thred.
was = stash.get();
}
// Must be 0.
return false;
}
}
Remove the bb and use only the aa object to create the two threads.
It's synchronized on this and you use two different objects (i.e. this values) - aa and bb. So practically you defeat the whole synchronization idea by using the two different objects.
Thread a = new Thread(aa);
Thread b = new Thread(aa);
a.start();
b.start();
Alternatively, you can do something like this.
public class MyRunnableClass implements Runnable {
private static final Object lock = new Object();
static int x = 30;
int y = 0;
#Override
public void run() {
for(int i=0;i<30;i++){
getFromStash();
}
}
public void getFromStash(){
synchronized(lock){
x--;
y++;
}
}
}
Here is what I think you want to achieve.
class Stash {
private int x = 30;
private int y = 0;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public synchronized void getFromStash(){
System.out.println("Method getFromStash called by " + Thread.currentThread().getName() + ".");
x--;
y++;
}
}
public class MyRunnableClass implements Runnable {
private Stash st = null;
private volatile boolean done = false;
public MyRunnableClass(Stash st){
this.st = st;
}
#Override
public void run() {
for(int i=0;i<30;i++){
this.st.getFromStash();
try {
double m = Math.random();
Thread.sleep((long)((m + 1) * 100.0));
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
System.out.println("Thread ---> " + Thread.currentThread().getName() + " finished!");
this.done = true;
}
public static void main(String[] args) throws Exception {
Stash st = new Stash();
MyRunnableClass aa = new MyRunnableClass(st);
MyRunnableClass bb = new MyRunnableClass(st);
Thread a = new Thread(aa);
Thread b = new Thread(bb);
a.setName("Thread A");
b.setName("Thread B");
a.start();
b.start();
while (true){
System.out.println(st.getX() + " " + st.getY());
Thread.sleep(10);
if (aa.done && bb.done) break;
}
System.out.println("Main thread finished too!");
}
}
Since you print the values right after you start the threads, you're not going to "catch" the threads in the middle of the for loops. The thread scheduler is returning control to the main thread sometimes after the threads are done and sometimes before they start, but never during run(). You have to wait until the threads are done.
As you've already figured out, your first attempt didn't work the way you wanted because 1) you weren't waiting for the threads to finish, so sometimes you read the values before they'd done their work, and 2) you're not looking for each thread to pull from the stash 30 times, but rather for the sum total of the pulls to be 30 (divided among the threads however it happens).
Your move to stopping each thread when x > 0 instead of after N pulls is the right approach, but the test for whether x > 0 (and therefore whether to continue) needs to be synchronized as well. Otherwise you could test the value and find that x == 1, decide to do a pull, and then before you actually do it the other thread takes the last one. Then you do your pull, leaving x at -1 and the sum of the two y's at 31.
To solve this, you either need to put a check for x > 0 within the synchronized getFromStash() method (so you don't actually change x and y unless it's safe to do so), or you need to expose the lock outside the Stash object from peter.petrov's answer, so that both threads can explictly synchronize on that object when they test x > 0 and then call getFromStash() if applicable.
Also, it's generally much harder to figure out thread synchronization when you're using static variables; there tend to be interactions you don't anticipate. You're much better off creating a separate object (e.g. peter.petrov's Stash class) to help you represent the pool, and the pass a reference to it to each of your thread classes. That way all access is via non-static references, and you'll have an easier time making sure you get the code right.

Java threads in order

I'm learning threads so I wanted to make a program which has two types of threads: one that writes random numbers and the other one which checks if the current number matches some specific number. The threads call write() and read(int) methods from the Numbers class. To make things more clear, I want my main program to look like this:
Numbers n = new Numbers();
new WritingThread(n);
new ReadingThread(n,3);
new ReadingThread(n,5);
So the output would be something like this:
2
7
3 !!! MATCH !!!
8
5 !!! MATCH !!!
1
...
The thing is that threads are not executed in order. I want to first execute the WritingThread, and then all the ReadingThreads. Because this way a new random number would be written and only one thread would have the chance to check if the numbers match. Here is the code:
class Numbers:
public class Numbers {
int number;
boolean written = false;
public synchronized void write() {
while (written)
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
number = (int) (Math.random() * 10);
System.out.print("\n" + number);
written = true;
notifyAll();
}
public synchronized void check(int n) {
while (!written)
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(" Reading thread: " + Thread.currentThread().getName());
if (n == number)
System.out.print(" !!! MATCH !!! ");
notify();
written = false;
}
}
class WritingThread:
public class WritingThread extends Thread {
Numbers n;
WritingThread(Numbers n){
this.n = n;
start();
}
public void run(){
while(true){
n.write();
}
}
}
class ReadingThread:
public class ReadingThread extends Thread{
Numbers n;
int number;
public ReadingThread(Numbers n, int number){
this.n = n;
this.number = number;
start();
}
public void run(){
while(true){
n.check(number);
}
}
}
And the output:
3 Reading thread: Thread-2
3 Reading thread: Thread-1 !!! MATCH !!!
0 Reading thread: Thread-2
5 Reading thread: Thread-1
0 Reading thread: Thread-2
0 Reading thread: Thread-1
5 Reading thread: Thread-2 !!! MATCH !!!
8 Reading thread: Thread-1
I know i could make one thread which has an array of numbers to check, but I am curious how could it be done this way. Thanks.
Lets start with your example. You have two consumers and one boolean flag. Think through the logic. Let's call our three threads W, C1 and C2.
W post 5
W set flag to true
W send notifyAll
C2 awake
C1 awake
C2 acquire lock
C1 block
C2 no match
C2 notify
W awake
W blocks
C2 release lock
C1 acquire lock
flag is false, C1 waits (releases monitor)
flag is false, C2 waits (releases monitor)
GOTO start
This is just one if the many possible ways in which this code can fun. Any time the lock needs to be acquired there is a free for all and of the threads waiting for it only one can get the lock. That thread will check the value set and reset the flag. If that thread is not the one that the value was intended for it is still consumed.
It should be fairly obvious that you have a race hazard. You are using a single queue for two consumer threads. Each consumer thread is fighting for the queue. Your queue is thread safe in that no more than one thread can read the single item from it at any one time but it causes a race hazard as each consumer thread expects to be the only one reading it. If the wrong thread reads the item then the other thread cannot see it.
The only way to resolve this is to have one queue per thread. The producer puts the same item into each consumer thread's private queue and each consumer thread takes items from its queue and reads them.
Here is an example using an ExecutorSerivce:
public static void main(String[] args) throws Exception {
final class Consumer implements Runnable {
private final BlockingQueue<Integer> q = new LinkedBlockingDeque<>();
private final int search;
public Consumer(final int search) {
this.search = search;
}
#Override
public void run() {
while (true) {
try {
if (q.take() == search) {
System.out.println("Found magic number.");
}
} catch (InterruptedException ex) {
return;
}
}
}
public Queue<Integer> getQ() {
return q;
}
}
final class Producer implements Runnable {
final Random r = new Random();
final Iterable<Queue<Integer>> qs;
public Producer(final Iterable<Queue<Integer>> qs) {
this.qs = qs;
}
#Override
public void run() {
while (true) {
final int i = r.nextInt();
for (final Queue<Integer> q : qs) {
q.offer(i);
}
}
}
}
final int numConsumers = 5;
final Collection<Queue<Integer>> qs = new LinkedList<>();
final ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < numConsumers; ++i) {
final Consumer c = new Consumer(i);
qs.add(c.getQ());
es.submit(c);
}
es.submit(new Producer(qs));
}
You are likely to get very few hits with this example as Random.nextInt() is used. If you want to get more hits reduce the range of the generated random numbers by calling Random.nextInt(int max) which generates numbers [0, max).
As you can see each Consumer has a queue of items to check and it blocks using the BlockingQueue API to wait for new items. The Producer puts the same item into each of the Consumer's queues in turn.

How to make one thread object stop the other thread object in java

I have a class here that will be used as a thread/Runnable object, the second class below (UseSearch) has a main method that intantiates two instances of the Search class and uses them to create two threads. As you can see, the run method calls the add method that runs the loop depending on the direction passed in. I am looking for a mechanism that will cause one of the threads to stop the other thread's loop from iterating when the other thread has finished running it's iteration. Any help/advise will be highly appreciated. I have seen a similar example but it's far too complex for me to comprehend. - Jevison7x
public class Search implements Runnable
{
int sum;
boolean direction;
String name;
public Search(String n, boolean positive)
{
this.direction = positive;
this.name = n;
}
void add()
{
if(direction == true)
{
for(int i = 0; i < 100; i++)
{
sum += 1;
System.out.println(name+" has "+sum);
}
}
else
{
for(int i = 0; i < 100; i++)
{
sum -= 1;
System.out.println(name+" has "+sum);
}
}
}
public void run()
{
add();
}
}
public class UseSearch
{
public static void main(String[] args)
{
Search s1 = new Search("bob", true);
Search s2 = new Search("dan", false);
Thread t1 = new Thread(s1);
Thread t2 = new Thread(s2);
t1.start();
t2.start();
}
}
The thread that is doing the iterating needs to test something on each iteration to see if it has been told to stop. That could be a custom flag (implemented in a variety of ways) or a the thread's interrupted flag.
If you are going to use interrupt, then Matt Clark's answer is half of the picture. The other half is that the iterating thread needs to do something like this:
if (Thread.currentThread.isInterrupted()) {
// pause or stop or break out of the loop or whatever
}
... in the body of the relevant loop or loops.
Note: there is no safe way in Java to stop or pause another thread that is not regularly checking to see if it should stop / pause; i.e. a thread that is not cooperating.
Long answer short...
Make the Threads class-wide variables so that each thread has access to the other:
Thread t1, t2;
public static void main(String[] args){
t1 = new Thread(){
public void run(){
t2.interrupt();
}
};
t2=new Thread(){
public void run(){
t1.interrupt();
}
};
}

synchronized Threads - Homework

I need to run a java program called ArrayHolder that will run two Threads. ArrayHolder will have an Array. ThreadSeven will overwrite every element of that Array with 7, and ThreadOne with 1.
The result after execution should be 7,1,7,1,7,1,7,1 etc. I have solved this problem, although I dont like my solution and was hoping you could suggest a better way.
p.s: Both Threads must write on all indexes.
public class ArrayHolder {
private int[] array = {1, 2, 3, 4, 5, 6, 4, 8, 9, 10};
public void writeInt(int pos, int num) {
array[pos] = num;
}
public static void main(String[] args) {
ArrayHolder holder = new ArrayHolder();
ThreadSeven seven = new ThreadSeven(holder, null);
Runnable one = new ThreadOne(holder, seven);
Thread thread1 = new Thread(seven);
Thread thread2 = new Thread(one);
seven.setThread(one);
thread1.start();
thread2.start();
holder.printArray();
}
private void printArray() {
for (int i = 0; i < 10; i++) {
System.out.println(array[i]);
}
}
public class ThreadSeven implements Runnable {
private ArrayHolder array;
private Runnable t;
private int flag=0;
#Override
public void run() {
for(int i=0;i<10;i++){
array.writeInt(i, 7);
flag=(flag+1)%2;
if (flag==0){
synchronized(t){
t.notify();
}
}else{
synchronized(this){
try {
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadSeven.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
public ThreadSeven (ArrayHolder ar,Runnable t){
array=ar;
this.t=t;
}
public void setThread(Runnable t){
this.t=t;
}
}
public class ThreadOne implements Runnable {
private ArrayHolder array;
private Runnable t;
private int flag = 0;
#Override
public void run() {
for (int i = 0; i < 10; i++) {
array.writeInt(i, 1);
flag = (flag + 1) % 2;
if (flag == 1) {
synchronized (t) {
t.notify();
}
} else {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadSeven.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
public ThreadOne(ArrayHolder ar, Runnable t) {
array = ar;
this.t = t;
}
public void setThread(Runnable t) {
this.t = t;
}
}
ThreadSeven and ThreadOne don't need to be separate classes; is looks like you just copy/pasted the code, and then changed the 7 in writeInt to a 1. Instead, you can paramaterize this value and pass it in the constructor. Then you get something like:
public class ThreadWriter implements Runnable {
private final int numberToWrite;
// ...
public ThreadOne(ArrayHolder ar, Runnable t, int numberToWrite) {
array = ar;
this.t = t;
this.numberToWrite = numberToWrite;
}
// ...
}
Another point is that both of your threads have to know about each other; this doesn't scale well. Pretend that for your next assignment your teacher said that you have to handle three threads which write 1, 4, 7, 1, 4, 7, ...; you would have to change the implementation of ThreadOne and ThreadSeven. A better solution which you could make now is have the ThreadWriters themselves be dumber, and manage their interaction more in the ArrayHolder class (or with an intermediary ThreadWriterManager class).
Your solution has some problems and looks to me like it would not print the correct result.
a) You don't wait for the threads to finish before you print the resulting array
Add thread1.join() and thread2.join() before holder.printArray() in case it is not there yet.
b) both threads start with writing immediately via array.writeInt(0, /* 1 or 7 */); After that they start to wait on each other. Whether the first index is correct or not depends on luck.
c) Continuing after this.wait(); without a loop checking a condition is not safe since the interrupt could be caused by something else than the other thread. I guess it's okay to do that here since it's just an exercise.
d) I see a potential deadlock: let's assume both threads are still writing the first index. So both are not in a synchronized block.
The thread that has to notify the other one does so, writes the next index and goes into it's own wait block.
But the second thread was not waiting at the time so the notify from the first thread did nothing. Second thread goes into wait block too.
Now both threads wait on each other and nothing happens anymore.
I don't have a great simple solution for you since that problem is quite complex.
The 1-thread needs to start writing at index 0 then wait until the 7-thread has written index 0 and 1, now 1-thread writes index 1 and 2 and waits and so on. That is the only way I see possible to ensure that both thread have written to every index and that the result is 7-1-7-1-.... Synchronizing access inside ArrayHolder would be very tricky since it needs to make sure that both threads have written to each index in the correct order.
But I think your general idea is okay. You just need to make sure that it is safe

Categories

Resources