How to signal a thread in java? - java

I have a Process running which creates a Thread object and runs it. Now based on some events in Process, I want Thread to react accordingly. More precisely if commonVar becomes true, then thread B should pause in while loop. Then when commonVar becomes false, then thread B should start from beginning of run() method.
Note that the thread B doesn't write to commonVar. It just reads it.
public class B implements runnable
{
Boolean commanVar;
public B(boolean commanVar) {
this.commonVar = commonVar;
}
public void run()
{
while(true) {
// do some processing
}
}
}
public class A
{
public static void main(String[] args) {
Boolean commonVar = false;
Thread threadB = new Thread(new ClassB(commonVar));
threadB.start();
// some processing will happen and because of that commonVar will change.
}
}

Do not stop threads. Instead, issue tasks (via BlockingQueue) or signals (by Semaphore), and let them run while they have input, and hang when input is exhausted.

Related

How to make Java runnable call a callback from the main thread, and not the background thread?

I was doing some thought experiment and here is my MyRunnable class:
class MyRunnable implements Runnable {
private final Integer mNumber;
private final CompleteHandler<Integer> mCallback;
public MyRunnable(Integer i, CompleteHandler<Integer> ch) {
mNumber = i;
mCallback = ch;
}
public void run() {
int sum = 0;
for (int i = 1; i <= mNumber; i++) {
sum += i;
}
mCallback.onFinished(sum);
}
}
This will be executed by a background thread which I create on the main thread, under the execute() method
public class Demo implements CompleteHandler<Integer>{
public static void main(String[] args) {
Demo d = new Demo();
d.execute();
}
#Override
public void onFinished(Integer i) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName); // thread-0
}
public void execute() {
MyRunnable mr = new MyRunnable(10, this);
Thread t = new Thread(mr);
t.start();
}
}
As you can see, the MyRunnable calls onFinished() when the task is finished. Is there any way I can have the background thread to call this on the main thread? I know I can do similar thing with callables, but right now I want to know if this is possible with runnables,
thank you
Johannes: Take a look at CompletableFuture...
Brendon: I'm more interested in seeing how it work on code
Here's a simplistic implementation that ignores the issue of exceptions. (Pardon me if it's not actually valid Java code.)
class CompletableFuture<ValueType> {
private Object lock = new Object();
private boolean is_completed = false;
private ValueType completed_value;
public synchronized void complete(ValueType v) {
completed_value = v;
is_completed = true;
notifyAll();
}
public synchronized ValueType await() {
while (! is_completed) {
wait();
}
return completed_value;
}
}
The idea is, the client thread creates a CompletableFuture instance, cf, and somehow passes it to the server thread, possibly along with other args that tell the server thread what to do. Then the client thread goes off to do other, unrelated things.
Meanwhile, the server thread does its thing, eventually produces a result, r, and then it calls cf.complete(r).
At some point, the client thread finishes doing whatever else it was doing, and now it needs the result, so it calls cf.await(). Either one of two things happen at that point:
The server already has set the is_completed flag, in which case, the client immediately gets the result, OR
The server has not yet finished, so the client goes in to the wait() loop to wait for it.
When you're looking at application code, you usually never see the part where the client thread creates the Future object or passes it to the other thread. That usually is all taken care of inside the library call when the client submits a task to a thread pool.

Java multi threading - run threads run method only once in sequence

In my applications there are an n number of actions that must happen, one after the other in sequence, for the whole life of the program. Instead of creating methods which implement those actions and calling them in order in a while(true) loop, I decided to create one thread for each action, and make them execute their run method once, then wait until all the other threads have done the same, wait for its turn, and re-execute again, and so on...
To implement this mechanism I created a class called StatusHolder, which has a single field called threadTurn (which signifies which thread should execute), a method to read this value, and one for updating it. (Note, this class uses the Singleton design pattern)
package Test;
public class StatusHolder
{
private static volatile StatusHolder statusHolderInstance = null;
public static volatile int threadTurn = 0;
public synchronized static int getTurn()
{
return threadTurn;
}
public synchronized static void nextTurn()
{
System.out.print("Thread turn: " + threadTurn + " --> ");
if (threadTurn == 1)
{
threadTurn = 0;
}
else
{
threadTurn++;
}
System.out.println(threadTurn);
//Wake up all Threads waiting on this obj for the right turn to come
synchronized (getStatusHolder())
{
getStatusHolder().notifyAll();
}
}
public static synchronized StatusHolder getStatusHolder()
{//Returns reference to this object
if (statusHolderInstance == null)
{
statusHolderInstance = new StatusHolder();
}
return statusHolderInstance;
}
}
Then I have, let's say, two threads which must be execute in the way explained above, t1 and t2.
T1 class looks like this:
package Test;
public class ThreadOne implements Runnable
{
#Override
public void run()
{
while (true)
{
ThreadUtils.waitForTurn(0);
//Execute job, code's not here for simplicity
System.out.println("T1 executed");
StatusHolder.nextTurn();
}
}
}
And T2 its the same, just change 0 to 1 in waitForTurn(0) and T1 to T2 in the print statement.
And my main is the following:
package Test;
public class Main
{
public static void main(String[] args) throws InterruptedException
{
Thread t1 = new Thread(new ThreadOne());
Thread t2 = new Thread(new ThreadTwo());
t1.start();
t2.start();
}
}
So the run method goes like this:
At the start of the loop the thread looks if it can act by checking the turn value with the waitForTurn() call:
package Test;
public class ThreadUtils
{
public static void waitForTurn(int codeNumber)
{ //Wait until turn value is equal to the given number
synchronized (StatusHolder.getStatusHolder())
{
while (StatusHolder.getTurn() != codeNumber)
{
try
{
StatusHolder.getStatusHolder().wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
If the two values are equal, the thread executes, otherwise it waits on the StatusHolder object to be awaken from the nextTurn() call, because when the turn value changes all the threads are awaken so that they can check if the new turn value is the one they are waiting for so they can run.
Note thatnextTurn() cycles between 0 and 1: that is because in this scenario I just have two threads, the first executes when the turn flag is 0, and the second when its 1, and then 0 again and so on. I can easily change the number of turns by changing this value.
The problem: If I run it, all goes well and seems to work, but suddenly the output console stops flowing, even if the program doesn't crash at all. I tried to put a t1.join() and then a print in the main but that print never executes, this means that the threads never stop/dies, but instead they remain locked sometimes.
This looks to be even more evident if I put three threads: it stops even sooner than with two threads.
I'm relatively new to threads, so I might be missing something really stupid here...
EDIT: I'd prefer not to delete a thread and create a new one every time: creating and deleting thousands of objs every second seems a big work load for the garbage collector.
The reason why I'm using threads and not functions is because in my real application (this code is just simplified) at a certain turn there actually are multiple threads that must run (in parallel), for example: turn 1 one thread, turn 2 one thread, turn 3 30 threads, repeat. So I thought why not creating threads also for the single functions and make the whole think sequential.
This is a bad approach. Multiple threads allow you to execute tasks concurrently. Executing actions "one after the other in sequence" is a job for a single thread.
Just do something like this:
List<Runnable> tasks = new ArrayList<>();
tasks.add(new ThreadOne()); /* Pick better names for tasks */
tasks.add(new ThreadTwo());
...
ExecutorService worker = Executors.newSingleThreadExecutor();
worker.submit(() -> {
while (!Thread.interrupted())
tasks.forEach(Runnable::run);
});
worker.shutdown();
Call worker.shutdownNow() when your application is cleanly exiting to stop these tasks at the end of their cycle.
you can use Semaphore class it's more simple
class t1 :
public class t1 implements Runnable{
private Semaphore s2;
private Semaphore s1;
public t1(Semaphore s1,Semaphore s2){
this.s1=s1;
this.s2=s2;
}
public void run()
{
while (true)
{
try {
s1.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(t1.class.getName()).log(Level.SEVERE, null, ex);
}
//Execute job, code's not here for simplicity
System.out.println("T1 executed");
s2.release();
}
}
}
class t2:
public class t2 implements Runnable{
private Semaphore s2;
private Semaphore s1;
public t2(Semaphore s1,Semaphore s2){
this.s1=s1;
this.s2=s2;
}
public void run()
{
while (true)
{
try {
s2.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(t2.class.getName()).log(Level.SEVERE, null, ex);
}
//Execute job, code's not here for simplicity
System.out.println("T2 executed");
s1.release();
}
}
}
class main:
public class Testing {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Semaphore s2=new Semaphore(0);
Semaphore s1=new Semaphore(1);
Thread th1 = new Thread(new t1(s1,s2));
Thread th2 = new Thread(new t2(s1,s2));
th1.start();
th2.start();
}}

How does Java Threads work

I'm a Java learner, trying to understand Threads.
I was expecting output from my program below, in the order
Thread started Run Method Bye
But I get output in the order
Bye Thread started Run Method
Here is my code:
public class RunnableThread
{
public static void main(String[] args)
{
MyThread t1= new MyThread("Thread started");
Thread firstThread= new Thread(t1);
firstThread.start();
System.out.println("Bye");
}
}
class MyThread implements Runnable
{
Thread t;
String s= null;
MyThread(String str)
{
s=str;
}
public void run()
{
System.out.println(s);
System.out.println("Run Method");
}
}
In a multithreaded code there's no guarantee which thread will run in what order. That's in the core of multithreading and not restricted to Java. You may get the order t1, t2, t3 one time, t3, t1, t2 on another etc.
In your case there are 2 threads. One is main thread and the other one is firstThread. It's not determined which will execute first.
This is the whole point of Threads - they run simultaneously (if your processor has only one core though, it's pseudo-simultaneous, but to programmer there is no difference).
When you call Thread.start() method on a Thread object it's similar (but not the same, as it's starting a thread, and not a process and former is much more resource consuming) simultaneously starting another java program. So firstThread.start() starts to run paralel to your main thread (which was launched by your main method).
This line starts a main execution thread (like a zeroThread)
public static void main(String[] args)
Which you can reference, by calling Thread.sleep() for example.
This line
firstThread.start();
Starts another thread, but in order to reference it you use it's name, but you reference it from the main thread, which is running paralel to firstThread.
In order to get the expected output you can join these two threads, which is like chaining them:
This way:
public static void main(String[] args)
{
MyThread t1= new MyThread("Thread started");
Thread firstThread= new Thread(t1);
firstThread.start();
firstThread.join();
System.out.println("Bye");
}
join(), called on firstThread (by main thread) forces main thread to wait until the firstThread is finished running (it will suspend proceding to the next command, which is System.out.println("Bye");).
It appears that you seek the thread (and probably more than one) to run while main() waits for everything to finish up. The ExecutorService provides a nice way to manage this -- including the ability to bail out after a time threshold.
import java.util.concurrent.*;
class MyThread implements Runnable { // ... }
class MyProgram {
public static void main(String[] args)
{
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
// At this point, 3 threads teed up but not running yet
ExecutorService es = Executors.newCachedThreadPool();
es.execute(t1);
es.execute(t2);
es.execute(t3);
// All three threads now running async
// Allow all threads to run to completion ("orderly shutdown")
es.shutdown();
// Wait for them all to end, up to 60 minutes. If they do not
// finish before then, the function will unblock and return false:
boolean finshed = es.awaitTermination(60, TimeUnit.MINUTES);
System.out.println("Bye");
}
}
There is no specified order in which Java threads are accustomed to run. This applies for all threads including the "main" thread.
If you really want to see it working, try:
class RunnableThread
{
public static void main(String[] args)
{
MyThread t1= new MyThread();
Thread firstThread= new Thread(t1);
firstThread.start();
System.out.println("Thread Main");
for(int i=1;i<=5;i++)
{
System.out.println("From thread Main i = " + i);
}
System.out.println("Exit from Main");
}
}
class MyThread implements Runnable
{
public void run()
{
System.out.println("Thread MyThread");
for(int i=1;i<=5;i++)
{
System.out.println("From thread MyThread i = " + i);
}
System.out.println("Exit from MyThread");
}
}
When You start a Thread it will execute in parallel to the current one, so there's no guarantee about execution order.
Try something along the lines:
public class RunnableThread {
static class MyThread implements Runnable {
Thread t;
String s= null;
MyThread(String str) {
s=str;
}
public void run() {
System.out.println(s);
System.out.println("Run Method");
}
}
public static void main(String[] args) {
MyThread t1= new MyThread("Thread started");
Thread firstThread= new Thread(t1);
firstThread.start();
boolean joined = false;
while (!joined)
try {
firstThread.join();
joined = true;
} catch (InterruptedException e) {}
System.out.println("Bye");
}
}
This is not a thread start order problem. Since you're really only starting a single thread.
This is really more of an API Call speed issue.
Basically, you have a single println() printing "bye", which gets called as soon as the Thread.start() returns. Thread.start() returns immediately after being called. Not waiting for the run() call to be completed.
So you're racing "println" and thread initializaiton after "thread.start()", and println is winning.
As a sidenote, and in general, you might want to try to use ExecutorService and Callable when you can, as these are higher level, newer APIs.

Gracefully shutdown a program with multiple threads having infinite while loop

I have a program which creates 10 threads, and each thread has an infinitely running while loop.
I need help to efficiently implement a Shutdown hook which can effectively STOP all the threads. Since I want to do a graceful shutdown each Thread should finish as soon as it finds the stop flag turned to TRUE.
public class SampleGSH implements Runnable{
private static boolean stop = false;
public static void main(String[] args) {
for(int i = 0; i < 10;i++) {
Thread t = new Thread(new SampleGSH(), "name"+i);
t.start();
}
}
#Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
public void run()
{
System.out.println("*******");
synchronized (this)
{
System.out.println("Turning switch off");
stop = true;
}
}
});
synchronized (this) {
while(!stop)
{
//Some logic which should not be killed abruptly once it starts running, a graceful shut down will not allow this code to start
}
}
}
}
Any help will be truly appreciated.
I need help to efficiently implement a Shutdown hook which can effectively STOP all the threads.
If you have any fields that are shared between multiple threads, they need to be synchronized. In this case your stop should be volatile. Without this, there is nothing that ensures that the threads will see the value of stop change to true. See this tutorial for information about atomic access.
See: Using boolean var for stopping threads
Couple other comments:
If you are starting a number of threads, you should consider using an ExecutorService
Your while loop is inside of a synchronized block. This does nothing and the stop field will not get memory synchronized since it gets updated externally while inside of the block.
Another way to stop a thread would be to interrupt() it. See this tutorial.
while (!thread.currentThread().isInterrupted()) {
...
}
...
t.interrupt();
Instead of a single static stop boolean, you could give every thread its own stop boolean. Then store all thread objects when creating them and set their stop boolean to true in the shutdown hook thread (which would be hooked in the main method).
Something like this:
import java.util.ArrayList;
import java.util.List;
public class SampleGSH extends Thread {
public boolean stop = false;
private static List<SampleGSH> threads = null;
public static void main(String[] args) {
threads = new ArrayList<SampleGSH>();
int numThreads = 10;
for (int i = 0; i < numThreads; i++) {
SampleGSH t = new SampleGSH();
threads.add(t);
t.start();
}
Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
public void run() {
System.out.println("*******");
for (SampleGSH t : threads) {
t.stop = true;
}
}
});
}
#Override
public void run() {
{
while (!stop) {
// Some logic which should not be killed abruptly once it starts
// running, a graceful shut down will not allow this code to
// start
}
}
}
}
Forget that addShutdownHook guff ... keep it simple ...
Make the static stop variable volatile ...
then add this method to SampleGSH ...
public void shutdown() {
stop = true;
}
then call it when you want to stop the threads!

Thread Executor - beginner

I have two classes. In class A, I have the run() method looped forever, while in the class B, i have the threadpool.
My question is, From Class B, how can I control and stop the thread executing run() method in class A , I have tried forceshutdown, threadExecutor.shutdownNow(), But it isnt working.
The loop seems to go on forever.
Here is example piece of code:
public class A implements Runnable {
public void run() {
while (true) {
System.out.println("Hi");
}
}
}
public class B {
public static void main(String[] args) {
int noOfThreads = 1;
A ThreadTaskOne = new A();
System.out.println("Threads are being started from Class B");
ExecutorService threadExecutor = Executors.newFixedThreadPool(noOfThreads);
threadExecutor.execute(ThreadTaskOne);
threadExecutor.shutdownNow();
System.out.println("B Ends, no of threads that are alive : " + Thread.activeCount());
}
}
As #MadProgammer said, your "infinite" loop needs to pay attention to Thread.isInterrupted. e.g. (very schematic)
public void run() {
while (!Thread.isInterrupted()) {
doSomethinginTheLoop1();
blah...blah...blah
// if the loop is very long you might want to check isInterrupted
// multiple times for quicker termination response
doSomethingInTheLoop2();
}
// now, here's a decision of what you do
// do you throw an InterruptedException or trust others to check interrupted flag.
// read Java COncurrency in Practice or similar...
}
The documentation on ExecutorService#shutdownNow() says -
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
And your thread doesn't seem to care if it has been interrupted.
So check if it has been interrupted
while (Thread.currentThread().isInterrupted())
instead of just doing
while (true)
May be below is useful for you.
public static class A implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Hi");
}
}
}

Categories