Stoping threads dynamicly in java - java

i need some help with threads , i need to make a program that will close and open threads dynamically ( meaning when i need a thread it will run, if its no longer needed it will stop ) ,
Now my questions is? is k if i only terminate the run method of that thread , does it count as the thread stopping, or how exactly do a close a thread .
public void run() {
while(!tasks.isEmpty()) {
try {
if(tasks.peek() != null) {
Thread.sleep(1000);
waitingPeriod.getAndAdd(-1);
tasks.peek().setProcessingTime(tasks.peek().getProcessingTime() - 1);
if(tasks.peek().getProcessingTime() == 0) {
// System.out.println("Removed task " + tasks.peek().getId() + " from queue" );
tasks.remove();
Thread.currentThread().interrupt();
continue;
}
}
}
catch(InterruptedException e) {
}
//take next task from queue
//stop the thread for a time equal with the task's processing time
//decrement the waitingPeriod
}
}
This threads works over a queue , if there are elements in it i start it from another class, and i want this thread to run only if i have elements, so when the queue empties i want the thread to stop.
how do i stop the thread , is enough to terminate the run function with that while?

Related

Timer issues in java when using Timeout Exceptions, code doesn't run correctly after a Timeout Exception

I'm designing a game in java called that's similar to the board game go. In the implementation there is also a mode called speed mode, where if a player doesn't take a turn within the time limit (5 seconds) the other player is to win. This mode can also be won normally be "capturing" the opposition piece. After either of these conditions is met , the game is to be ran again from the main menu. This works fine in normal mode and in speed mode when the win conditions are met by capturing. When it's won by time running out however it behaves very oddly, almost randomly prompting for input and printing.
The code for the time is as follows:
public Boolean speedMode(Player player, Player opponent) {
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Runnable r = new Runnable() {
Boolean outOfRange;
public void run() {
do {
outOfRange = takeTurn(player);
} while (outOfRange == true);
}
};
Future<?> f = service.submit(r);
f.get(5, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
System.out.println("The thread was interrupted during sleep, wait or join");
} catch (final TimeoutException e) {
player.setWon(false);
System.out.println("\n" + player.getName() +", you took too long... ");
return true;
} catch (final ExecutionException e) {
System.out.println("An exception from within the Runnable task");
}
return false;
}
When the TimeoutException happens the oppisite player wins while loop shown beow is exited and the correct congratulations are printed. The problem is when It starts the new game at the bottom line of code thats when the weird behaviour starts. Is there something I need to close perhaps in the timer method?It's almost like it's still running in the back ground.
else {
do {
timeOut = speedMode(second, first);
if(winCheck1(first) == true || timeOut == true){
break;
}
timeOut = speedMode(first, second);
} while (winCheck1(second) != true && timeOut != true);
if(player1.isWon() == true){
System.out.println("\n\nCongratulations " + player1.getName() + " you are the winner!\n\n");
}
else{
System.out.println("\n\nCongratulations " + player2.getName() + " you are the winner!\n\n");
}
}
//reload the menu
Game game = new Game();
}
Basically my question is; can anyone tell me why starting a new game does not work correctly after throwing a TimeoutException?
Is there something I need to close perhaps in the timer method?It's almost like it's still running in the back ground.
If you don't call shutdown on the executor the worker thread that the executor created will hang around. (This is independent of the scope in which you declared the executor.) If it is a non-daemon thread then it will keep the old JVM alive.
Also your task needs to be responsive to interruption. See the documentation for shutdownNow:
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.
"Responding to interrupts" means checking Thread.currentThread().isInterrupted() to see if the thread has been canceled and then acting on it (finding a stopping place, doing cleanup, and exiting the run method), and restoring the interrupted flag (Thread.currentThread().interrupt()) if an InterruptedException or InterruptedIOException is caught.

Simple java timer not working

I am new to programming and i used following code to run a timer. When this method is called with boolean run = trueit is working fine but not stops when boolean run = false. How can i solve this ?
private void countTime(boolean run){
new Thread(){
public void run(){
int mm = 00;
int hh = 00;
while(run){
for (int i=1 ;i<61 ;i++){
try{
int ss = i;
Thread.sleep(1000);
if (ss == 60){
mm += 1;
if (mm == 60){
}
}
if (mm == 60){
hh += 1;
mm = 0;
}
if (ss < 10){
lblClock.setText(Integer.toString(hh) + ":" +
Integer.toString(mm) + ":" + "0"
+ (Integer.toString(ss))
);
} else {
lblClock.setText(Integer.toString(hh) + ":" +
Integer.toString(mm) + ":"
+ (Integer.toString(ss))
);
}
} catch (InterruptedException ex) {
}
}
}
}
}.start();
}
I am guessing that you are starting the timer like countTime(true) and to turn it off you are calling countTime(false).
You did not notice that the timer is running as separate thread. So calling again you are launching a new thread. Basically each call will launch a new thread, nothing will be updated in any of the previously running threads.
This is a anonymous Thread class which creates a new thread every time you call the countTime(boolean) method.
Can you add "caller" code & code snippet setting run as false?
From current code, countTime starts a new Thread always. That may be the reason. Did not see where "run" was set as false.
Do you want to start new thread always? and How and where are you making run as false?
I think that you need only one thread and you have to set run value "true" or "false" from the caller. Posting caller code will help to fix the problem.
EDIT:
1) Declare one thread separately. Provide setter and getter method for "run" boolean variable.
2) When user click on start button click of "start", start the thread with run as true. If user clicks on start twice, make sure that second thread is not created. If (thread == null) create new thread and ignore the request if thread is not null
3) When user click on stop button & thread is not null, set the run as false.
By calling countTime(false),you just start another thread.if you print the time message to the console and click start button twice,you will find there are two timer running.
To stop the thread,you might do like this:
Thread thread=new Thread(){
public void run(){
//your timer code here
}};
public synchronized void stop(){
if(null != thread){
thread.interrupt();
thread = null;
notifyAll();
}
}
public void start(){
if(null == thread){
thread = new Thread(this);
thread.start();
}
}
when you want to start the thread,call start(),and stop() to stop it.
why doing so?when the thread is in the blocked state (by calling Thread.sleep(1000)) then call thread.interrupt() to let it throw the InterruptedException.
I am new to programming too,and had the similar problem,hope that can help you

Join()ing on multiple threads and handling leaked output

I'm solving a multi-threaded problem in Java and have the core problem itself resolved, but my output isn't what I'd expect.
I have a main thread that spins new threads that each perform their tasks. It looks like the following (pseudocode):
initializeThreads(); // Creates the new threads
startThreads(); // Starts the new threads
sleep( duration ); // Lets the threads run for `duration` time
shutdownThreads(); // interrupt the threads
printOutput(); // output the results to the console
// definition of shutdownThreads() is at the end of my question
My issue happens upon trying to join() on each thread in my list of threads. Every now and then my program gets caught in an endless loop because I'm guessing the interrupt() I call on my list of threads doesn't happen fast enough compared to the join() and not all the threads get interrupted.
My other issue occurs when the program does shutdown the threads correctly and print the termination output. While the program is running each thread has messages that it logs to the console, and after I interrupt the threads and display the final program message some of these thread-specific logging messages leak out to the console.
For example, let's say one of my threads outputs "Thread-1 waiting for lock on object..." while it runs, and the main program finally wakes itself up, terminates the thread, and outputs "Program finished". Sometimes, the thread-specific message will show up after the program termination message.
If anyone could help me figure out how to stop this from happening please let me know!
shutdownThreads()
private void shutdownThreads() {
Thread t;
for (int i = 0; i < threads.size(); i++) {
t = threads.get(i);
t.interrupt();
}
for (int i = 0; i < threads.size(); i++) {
t = threads.get(i);
try {
t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted while waiting on thread to exit");
}
}
}
EDIT: One thing I thought of doing is rewriting shutdownThreads() to do this:
for (int i = 0; i < threads.size(); i++) {
Thread t = threads.get(i);
t.interrupt();
while (!t.isInterrupted()) ;
}
but this doesn't seem too elegant.
Your edited code at the bottom, that's gonna be a forever loop before it iterates the for() twice.

start a thread which terminates the process after a specified time

I am executing a program for a network where i have a certain number of tasks execution in loop, it works fine but when there a any flaws occurs due to network problem it got stuck in one of any task. so i want to create a thread which start at the time when control goes in to loop and after some delay it terminate it self with continuing the process.
for example-
for ( /*itearting condition */)
{
//thread start with specified time.
task1;
task2;
task3;
//if any execution delay occur then wait till specified time and then
//continue.
}
Please give me some clue regarding this, a snippets can help me a lot as i need to fix it shortly.
A thread can only be terminated with its cooperation (assuming you want to save the process). With the thread's cooperation, you can terminate it with any termination mechanism it supports. Without its cooperation, it cannot be done. The usual way to do it is to design the thread to sanely handle being interrupted. Then you can have another thread interrupt it if too much time passes.
I think you may need something like this:
import java.util.Date;
public class ThreadTimeTest {
public static void taskMethod(String taskName) {
// Sleeps for a Random amount of time, between 0 to 10 seconds
System.out.println("Starting Task: " + taskName);
try {
int random = (int)(Math.random()*10);
System.out.println("Task Completion Time: " + random + " secs");
Thread.sleep(random * 1000);
System.out.println("Task Complete");
} catch(InterruptedException ex) {
System.out.println("Thread Interrupted due to Time out");
}
}
public static void main(String[] arr) {
for(int i = 1; i <= 10; i++) {
String task = "Task " + i;
final Thread mainThread = Thread.currentThread();
Thread interruptThread = new Thread() {
public void run() {
long startTime = new Date().getTime();
try {
while(!isInterrupted()) {
long now = new Date().getTime();
if(now - startTime > 5000) {
//Its more than 5 secs
mainThread.interrupt();
break;
} else
Thread.sleep(1000);
}
} catch(InterruptedException ex) {}
}
};
interruptThread.start();
taskMethod(task);
interruptThread.interrupt();
}
}
}

ExecutorService never stops without Exceptions

I adopted a the concurrency strategy from this post. However mine looks like this:
ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_CREATE_KNOWLEDGE_THREADS);
List<Callable<Collection<Triple>>> todo = new ArrayList<Callable<Collection<Triple>>>(this.patternMappingList.size());
for (PatternMapping mapping : this.patternMappingList ) {
todo.add(new CreateKnowledgeCallable(mapping, i++));
}
try {
List<Future<Collection<Triple>>> answers = executorService.invokeAll(todo);
for (Future<Collection<Triple>> future : answers) {
Collection<Triple> triples = future.get();
this.writeNTriplesFile(triples);
}
}
catch (InterruptedException e) { ... }
catch (ExecutionException e) { ... }
executorService.shutdown();
executorService.shutdownNow();
But the ExecutorService never shuts down. I tried to debug how many of the CreateKnowledgeCallable are finished, but this number seems to vary (after no new threads/callables are executed but the service keeps running). I am sure a logged and printed every possible exception but I can't see one happening. It also seems that after a while nothing happens anymore except that NUMBER_OF_CREATE_KNOWLEDGE_THREADS cpus are spinning at 100% forever. What am I doing wrong?
If you need to more specific infos I would be happy to provide them for you!
Kind regards,
Daniel
When you perform a shutdownNow() it interrupts all the threads in the pool. However, if your code ignores interrupts, they won't stop. You need to make your tasks honour interrupts with tests like
while(!Thread.currentThread.isInterrupted()) {
}
or
Thread.sleep(0);
executorService.invokeAll
should return only when all tasks are finished. As well as future.get()
Are you sure, that call to executorService.invokeAll(todo); ever returns and not blocks forever waiting for tasks to complete?
are you sure that you submitted tasks actually finish? If you check the API for shutdownNow() and shutdown() you'll see that they do not guarantee termination.
Have you tried using a call to awaitTermination(long timeout,
TimeUnit unit) with a reasonable amount of time as timeout parameter? (edit: "reasonable amount of time" depends of course on the mean process time of your tasks as well as the number of tasks executing at the time you call for termination)
Edit2: I hope the following example from my own code might help you out (note that it probably isn't the optimal, or most gracious, way to solve this problem)
try {
this.started = true;
pool.execute(new QueryingAction(pcqs));
for(;;){
MyObj p = bq.poll(timeout, TimeUnit.MINUTES); // poll from a blocking queue
if(p != null){
if (p.getId().equals("0"))
break;
pool.submit(new AnalysisAction(ds, p, analyzedObjs));
}else
drc.log("Timed out while waiting...");
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
drc.log("--DEBUG: Termination criteria found, shutdown initiated..");
pool.shutdown();
int mins = 2;
int nCores = poolSize -1 ;
long totalTasks = pool.getTaskCount(),
compTasks = pool.getCompletedTaskCount(),
tasksRemaining = totalTasks - compTasks,
timeout = mins * tasksRemaining / nCores;
drc.log( "--DEBUG: Shutdown commenced, thread pool will terminate once all objects are processed, " +
"or will timeout in : " + timeout + " minutes... \n" + compTasks + " of " + (totalTasks -1) +
" objects have been analyzed so far, " + "mean process time is: " +
drc.getMeanProcTimeAsString() + " milliseconds.");
pool.awaitTermination(timeout, TimeUnit.MINUTES);
}
Everyone with this sort of problems should try to implement the same algorithm without concurrency. With the help of this method, I found that a component has thrown a runtime exception which was swallowed.

Categories