I need a thread that will only run once at a time, for example if it's called for the first time it will run, if it is called a second time, the first should stop completely and be allowed to die and a new one should take it's place.
I was ran a small test to see what was actually happening between each execution, the results show that the thread doesnt die but instead two threads are being executed alongside:
public class Test {
Worker worker = new Worker();
#Override
public void valid() {
try {
if (worker.running) {
worker.running = false;
worker.join();
}
} catch (InterruptedException iex) {
worker.running = false;
}
worker = new Worker();
worker.start();
}
private final class Worker extends Thread {
private volatile boolean running = true;
#Override
public void run() {
while (running) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException iex) {
Thread.currentThread().interrupt();
}
}
}
}
}
The results are as follows:
//Upon first execution
Thread-4
Thread-4
Thread-4
Thread-4
//When I execute it again
Thread-7
Thread-4
Thread-7
Thread-4
Thread-7
Thread-4
I've tried using ExecutorService or using while(!Thread.currentThread.isInterrupted) instead of the boolean flag, and got the same results.
How can I properly stop "Thread-4" and have only one of them running?
The actual issue comes from a thread that will cycle through a list and update things on discord chat by request, what the thread does is listen to input and change as suggested by kidney I'm trying to use executor.submit() and Future
private ExecutorService executor = Executors.newSingleThreadExecutor();
private Future<Void> worker;
private void setupImageThread() {
if (!worker.isDone() && !worker.isCancelled()) {
worker.cancel(true);
}
this.worker = (Future<Void>)executor.submit(new Cycler(Listener.queue(), this.links, Cel.cMember()));
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
Runnable timeout = () -> {
executor.shutdown();
};
ses.schedule(timeout, 100, TimeUnit.SECONDS);
}
How can I go about initializing the Future for the first time it is created?
Using single thread executor service, I would try something like this:
public class Test {
private static ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Void> worker;
public Test() {
this.worker = executor.submit(new Worker());
}
#Override
public void valid() {
if (!worker.isDone() && !worker.isCancelled()) {
worker.cancel(true); // Depends on whether you want to interrupt or not
}
this.worker = executor.submit(new Worker());
}
}
And make Worker implement Runnable.
It seems that the method valid can be called several times simultaneously. That means, every of those calls will wait to end only for one thread (Worker), whereas, every of them creates its own Worker and you lose a pointer to it, so it impossible to stop bunch of new created workers.
You should make the valid method synchronized: synchronized void valid() it will prevent creating many workers:
#Override
synchronized public void valid() {
...
}
One more thing to say. You put the while loop outside the try-catch, which is wrong: if the tread gets interrupted, the interruption doesn't kill it, because next interation gets started, so it should be like that:
#Override
public void run() {
try {
while (running) {
System.out.println(Thread.currentThread().getName());
Thread.sleep(2000);
}
catch (InterruptedException iex) {
//you don't need here Thread.currentThread().interrupt() call, because the thread has alredy been interrupted.
// The return statement here is also obsolete, I just use it as an example, but you can use empty braces.
return;
}
}
}
Related
I have made a class called AbortableThread that is supposed to start and stop a thread when I want to. The class is relatively small since it just contains this code :
public class AbortableThread implements Runnable
{
private Thread worker;
private Runnable target;
public AbortableThread(Runnable target)
{
this.target = target;
}
public void start()
{
worker = new Thread(this);
worker.start();
}
public void stop()
{
worker.interrupt();
}
public void run()
{
while (!worker.isInterrupted())
target.run();
}
}
However, calling stop() does not stop the Thread. I think that's because target.run() runs on a separate thread, but I have no clue.
There isn’t a good way to stop a Runnable from outside (as a last resort there is stop, but that isn’t a good way).
A Runnable needs to be a good citizen and check for interruption itself, using Thread.currentThread().isInterrupted().
If the Runnable catches InterruptedException, the interrupt flag will be cleared; the Runnable needs to restore the interrupt flag by calling interrupt on the current thread.
In the posted code what happens is that the Runnable executes on the worker thread and the worker never gets a chance to check the interruption flag until the Runnable completes. Assuming the Runnable is something like
() -> { try {
Thread.sleep(100000L):
} catch (InterruptedException e) {}}
then the sleep would be cut short when the worker is interrupted, but the interrupt flag would be cleared so the Runnable would be executed again in the next iteration of the loop.
Try something like this:
public class AbortableThread implements Runnable
{
private final AtomicBoolean running = new AtomicBoolean(false);
private Thread worker;
private Runnable target;
public AbortableThread(Runnable target)
{
this.target = target;
}
public void start()
{
worker = new Thread(this);
worker.start();
}
public void stop()
{
running.set(false);
}
public void run()
{
running.set(true);
while (running.get()) {
try {
// make an small sleep
Thread.sleep(1);
} catch (InterruptedException e){
Thread.currentThread().interrupt();
System.out.println(
"Thread was interrupted, Failed to complete operation");
}
// do something here like the one in question; it must be none-blocking
target.run();
}
}
}
A complete example can be found here:
How to Kill a Java Thread
I want to run a few tasks simultaneously so I have a code that looks like this:
for(final Task task : tasks){
(new Thread(){public void run(){
task.run(args);
}}).start();
How can I know when all of the tasks are done (the amount of tasks can vary) so that I can run something only after everything is done?
System.out.println("All tasks are finished");
A shorter version is to use a parallelStream
tasks.parallelStream().forEach(t -> t.run(args));
This will run all the tasks, using all the CPUs you have (if you have enough tasks) and wait for the all to finish.
Instead of creating Threads explicitly, submit your Runnables to an ExecutorService, then call its shutdown and awaitTermination methods:
ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
for (final Task task : tasks) {
executor.submit(new Runnable() {
#Override
public void run() {
task.run(args);
}
});
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLSECONDS);
You can also use a CountDownLatch to indicate if the threads are complete. See https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
Like so:
public static class Task {
void run(String args[]) {
System.out.println("Hello");
}
}
public static void main(String[] args) {
List<Task> tasks = Arrays.asList(new Task(), new Task(), new Task());
CountDownLatch doneSignal = new CountDownLatch(tasks.size());
for(final Task task : tasks) {
(new Thread() {
public void run(){
task.run(args);
}
}).start();
doneSignal.countDown(); //counts latch down by one
}
//A
new Thread() {
public void run() {
try {
doneSignal.await(); //blocks until latch counted down to zero
System.out.println("All tasks completed");
} catch(Exception e) {
System.out.println("Warning: Thread interrupted.");
}
}
}.start();
}
Each task thread will count the latch down by one when they complete. The Thread created at //A will wait until the latch has been count down to zero. Only then, the "All tasks completed" statement will be printed. So basically the statement after doneSignal.await() will only execute after all threads have completed.
Use CyclicBarrier , example :
http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html
Simply add each thread to a list when you create it:
LinkedList<Thread> threads = new LinkedList<>();
And when you make a new thread:
Thread t = new Thread() {
public void run() {
task.run(args);
}
}
threads.add(t);
Then you can check each thread in your list to see if it is finished:
boolean allFinished = true;
for (Thread t : threads) {
if (t.isAlive()) {
allFinished = false;
break;
}
}
See this link for more info on checking if threads are finished:
How to know if other threads have finished?
The key point in this questions is that if you want to check on threads later, you MUST store a list (or array, etc) of them somewhere.
Another way is to use the Future interface. Doing it this way gives you some other nifty features. See this for some examples.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Testing a multithreaded Java class that runs the threads sequentially
Please don't treat this below question as a duplicate one..!
I have developed a class that lets multi-threads to run sequentially, one at a time and in order. All the application code between this class' claimAccess function and release Access function will be executed only in one thread at one time. All other threads will wait in the queue until the previous thread completed.Please advise I want to test my class by writing a piece of code in main() method itself .
import java.util.ArrayList;
import java.util.List;
public class AccessGate {
protected boolean shouldWait = false;
protected final List waitThreadQueue = new ArrayList();
/**
* For a thread to determine if it should wait. It it is, the thread will
* wait until notified.
*
*/
public void claimAccess() {
final Thread thread = getWaitThread();
if (thread != null) {
// let the thread wait untill notified
synchronized (thread) {
try {
thread.wait();
} catch (InterruptedException exp) {
}
}
}
}
/**
* For a thread to determine if it should wait. It it is, the thread will be
* put into the waitThreadQueue to wait.
*
*/
private synchronized Thread getWaitThread() {
Thread thread = null;
if (shouldWait || !waitThreadQueue.isEmpty()) {
thread = Thread.currentThread();
waitThreadQueue.add(thread);
}
shouldWait = true;
return thread;
}
/**
* Release the thread in the first position of the waitThreadQueue.
*
*/
public synchronized void releaseAccess() {
if (waitThreadQueue.isEmpty()) {
shouldWait = false;
} else {
shouldWait = true;
// give the claimAccess function a little time to complete
try {
Thread.sleep(10);
} catch (InterruptedException exp) {
}
// release the waiting thread
final Thread thread = (Thread) waitThreadQueue.remove(0);
synchronized (thread) {
thread.notifyAll();
}
}
}
}
Now my main method would be ..
public static void main (String args[])
{
}
please advise how I spawn thr threads in my my main method to test the above class..!!Please advise
This should get you started...
public static void main (String args[])
{
AccessGate gate = new AccessGate();
// create as many threads as you like
Thread t1 = new MyThread(gate);
Thread t2 = new MyThread(gate);
// start all the threads you created
t1.start();
t2.start();
}
class MyThread extends Thread {
AccessGate gate;
public MyThread(AccessGate g) {
gate = g;
}
public void run() {
gate.claimAccess();
// Do something or print something.
// Could output several statements.
// Why not do a sleep as well to see if other threads interrupt
// this code section.
gate.releaseAccess();
}
}
Consider using Executors.newSingleThreadExecutor(). This is a thread pool with only one thread executing tasks. Next task will start execution only after first task is finished:
Executor executor = Executors.newSingleThreadExecutor();
Future<String> future1 = executor.submit(new Callable<String>() {
#Override
String call() throws Exception {
// my first task
}
});
Future<String> future2 = executor.submit(new Callable<String>() {
#Override
String call() throws Exception {
// my second task
}
});
...
You can retrieve result of task execution via Future API, also it allows you to track status of each job.
I have some struggle with threads in Java, I have three threads - thread1, thread2, and thread3. Those are doing some task when it started, I want to stop these two threads by thread1. I put thread1 for sleep(500), then I stop the both threads, but the process of two threads are still running. Do you have any idea how to do this?
How're you attempting to stop them? Thread.stop? Be warned that this method is deprecated.
Instead, look into using some sort of flag for thread 1 to communicate to thread 2 and 3 that they should stop. In fact, you could probably use interrupts.
Below, Thread.interrupt is used to implement the coordination.
final Thread subject1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
Thread.yield();
}
System.out.println("subject 1 stopped!");
}
});
final Thread subject2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
Thread.yield();
}
System.out.println("subject 2 stopped!");
}
});
final Thread coordinator = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException ex) { }
System.out.println("coordinator stopping!");
subject1.interrupt();
subject2.interrupt();
}
});
subject1.start();
subject2.start();
coordinator.start();
Alternatively, you could also use a volatile boolean (or AtomicBoolean) as means of communicating.
Atomic access provided by volatile and java.util.concurrent.atomic.* allow you to ensure mutation of the flag is seen by the subject threads.
final AtomicBoolean running = new AtomicBoolean(true);
final ExecutorService subjects = Executors.newFixedThreadPool(2);
subjects.submit(new Runnable() {
public void run() {
while (running.get()) {
Thread.yield();
}
System.out.println("subject 1 stopped!");
}
});
subjects.submit(new Runnable() {
public void run() {
while (running.get()) {
Thread.yield();
}
System.out.println("subject 2 stopped!");
}
});
final ScheduledExecutorService coordinator = Executors.newSingleThreadScheduledExecutor();
coordinator.schedule(new Runnable() {
public void run() {
System.out.println("coordinator stopping!");
running.set(false);
subjects.shutdown();
coordinator.shutdown();
}
}, 500, TimeUnit.MILLISECONDS);
Similarly, you could opt to, rather than use AtomicBoolean, use a field such as:
static volatile boolean running = true;
Better yet, if you take advantage of ExecutorServices, you can also program similar code as follows:
final ExecutorService subjects = Executors.newFixedThreadPool(2);
subjects.submit(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
Thread.yield();
}
System.out.println("subject 1 stopped!");
}
});
subjects.submit(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
Thread.yield();
}
System.out.println("subject 2 stopped!");
}
});
final ScheduledExecutorService coordinator = Executors.newSingleThreadScheduledExecutor();
coordinator.schedule(new Runnable() {
public void run() {
System.out.println("coordinator stopping!");
subjects.shutdownNow();
coordinator.shutdown();
}
}, 500, TimeUnit.MILLISECONDS);
This takes advantage of the fact that ThreadPoolExecutor.shutdownNow interrupts its worker threads in an attempt to signal shutdown.
Running any example, the output should be something to the effect of:
C:\dev\scrap>javac CoordinationTest.java
C:\dev\scrap>java CoordinationTest
coordinator stopping!
subject 1 stopped!
subject 2 stopped!
Note the last two lines can come in either order.
You can't stop a thread from another thread. You can only ask the thread to stop itself, and the best way to do that is to interrupt it. The interrupted thread must collaborate, though, and respond to the interruption as soon as possible by stopping its execution.
This is covered in the Java tutorial about concurrency.
You can either:
Have some boolean flag which the threads check regularly. If it is changed, then, they stop executing (note this can cause race conditions)
Another option would be to use the ExecutorService:
An Executor that provides methods to manage termination and methods
that can produce a Future for tracking progress of one or more
asynchronous tasks.
I have a JRuby engine which evaluates some scripts and I want to close the thread if it takes more than 5 seconds.
I tried something like this:
class myThread extends Thread{
boolean allDone = false;
public void threadDone() {
allDone = true;
}
public void run() {
while(true) {
engine.eval(myScript);
if(allDone)
return;
}
}
(...)
th1 = new myThread();
th1.start();
try {
Thread.sleep(5000);
if(th1.isAlive())
th1.threadDone();
} catch(InterruptedException e) {}
if(th1.isAlive())
System.out.println("Still alive");
I also tried to kill the thread with th1.stop() or th1.interrupt() but the value retured by th1.isAlive() method is always true.
What can I do?
I want to add that myScript could be "while(1) do; end" and I cannot wait until it's completed. So I want to prevent scripts like that and kill the thread if it takes more than 5 seconds.
Another solution would be to use the built-in mechanism to interrupt threads:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
engine.eval(myScript);
}
}
...
th1 = new myThread();
th1.start();
try {
Thread.sleep(5000);
th1.interrupt();
}
This way, no need for an allDone field, and no risk in failing to synchronize.
To make your Thread stoppable you might want something like.
class MyTask implements Runnable {
public void run() {
try {
engine.eval(myScript);
} catch(ThreadDeath e) {
engine = null; // sudden death.
}
}
}
You can call Thread.stop(), but I suggest you read the warnings on this method first.
If you want a thread to run for up to 5 seconds, the simplest solution is for the thread to stop itself.
class MyTask implements Runnable {
public void run() {
long start = System.currentTimeMillis();
do {
engine.eval(myScript);
} while(System.currentTimeMillis() < start + 5000);
}
}
This assumes you want to run engine.eval() repeatedly. If this is not the case you may have to stop() the thread. It is deprecated for a good reason but it might be your only option.