Stopping & restarting threads
ive got a thread i want to be able to turn on and off.
is there anyway i can keep the same thread, and just keep starting/stopping it, without it having to be stuck in a while loop, or stuck in sleep.
or, is there a way i can wake it up from sleep?
I figured it might have been something like that.. but how should i go about using it?
interrupt() when im not using it, and then start() it again?
sorry about the question, im not able to test it out myself at the moment :)
Similar Messages
-
How to stop main thread ?
Hi,
Inside my java class, after I launch a GUI, I want to stop this main thread. After user make some choice and close GUI window, then, I want to go back to main thread. I use wait() method inside my class to stop main thread , but it does not work and it give me "IllegalMonitorStateException" error. I met same thing, when user close the GUI window and call method notifyAll(). How to stop main thread for a while and how to go back?? Thanks
GaryHi,
you can create a boolean, and create a while loop, with a Thread.sleep(time); when you want to continue, you just have to change the state of your boolean. So you don't hava to exit the main. And you can't restart a run() in a thread. You can run it only once, so try to keep in your run() with an appropriate loop.
Hope it helps.
S�bastien -
I restarted my iphone after it wouldnt stop restarting every 5 seconds so i plugged it into i tunes backed it up and restored it but when i did my phone had no data and that back up i made is gone... is there anyway i can get this back please eveything is gone, 1,700 photos 500 contacts all my emails and texts. i have a Time Machine can i restore itunes that way and have it?
I suggest plugging your iPhone into a wall socket, and letting it sit for many hours (while connected to WiFi).
-
My iPad died and i plugged it in and now it won't stop restarting itself, it goes apple logo, loading, shut off and then it repeats itself. I thought that maybe the iPad got just enough power to start restarting but then died midway through and that just kept continuously happening.
Try a reset: Hold down the power and Home buttons together until the Apple logo is displayed, then release. No data or settings are lost.
-
Hi,
How to stop the thread in java. This is my program.
import java.net.InetAddress;
public class ThreadPing extends Thread {
ThreadPing(String pingIP)
super(pingIP);
start();
public void run()
try
String pingIP = Thread.currentThread().getName();
InetAddress inet = InetAddress.getByName(pingIP);
Boolean get=inet.isReachable(1500);
if(get==true)
System.out.println(inet.getHostName());
}catch(Exception e)
public static void main(String args[])
for(int i=1;i<=100;i++)
String pingIP = "192.168.1."+i;
ThreadPing tp = new ThreadPing(pingIP);
Thanks in advance.The simplest way to stop all the thread is to make all thread daemons and exit the program when you want them to stop.
-
Stopping a Thread in Infinite Loop
I've read several articles on how to stop Threads, and all of them point to using Thread.interrupt(). The problem right now is what happens when the thread is in an infinite loop. For example:
class A implements Runnable
public void run()
while(!Thread.currentThread().isInterrupted())
while(true);
//in other class's main method:
Thread a = new Thread(new A());
a.start();
a.interrupt();The a.interrupt() call only sets the isInterrupted flag in Thread, but it does not terminate the thread as a.stop() would. However, stop() throws a ThreadDeath exception that I would not want to have to deal with. Is there any way to stop this infinite loop thread safely?
Thanks in advance!No need to get snitty. You certainly did not make clear that you are not a newbie at programming. Plenty of newbies who barely have a grasp of the language fundamentals post thread questions here. I thought I did address the question at hand. It seems I misunderstood what you were asking.
The only way to safely stop that inner loop is like so: while (...) {
while (!done) {
}where done is volatile, or all access to it is sychronized on the same lock (meaning you'd sync the !done check above as well).
If you can't do that, and it's stuck at while (true) and you can't modify the body of the inner loop to check done, then you're SOL.
(I suppose it's conceivable that 1.6 or 6.0 or whatever it's called will introduce some new safe way to stop that thread, but I haven't heard anything about it.) -
How to stop a thread in java 1.5 on windows
Hi All,
I am using Java 1.5 on windows plateform. I want to stop all the threads which belongs to a particular process when the timeout occurs. In java 1.5 stop() method is depricated and interrupt method just sets the flag and does not stop the thread actually.
Is there any way to destroy the thread permenently. I am using TheadPool Executor class.
Regards
Rinku GargHi,
I am having a timer task which is scheduled to run after some fixed time interval when the process started.
Now this timer task when started, should destroy the active threads of the request. When the request is timed out then the thread is action should termininate.
In my case run method of particular thread had already made a call to Database through DAO when the time out occurs. So how can I set up a loop in run method which I found on google.
thread.stop() is deprecated.
thread.destroy() is deprecated.
I used thread.interrupt() but it does not stops the thread.
Please help me in this scenario.
Regards
Rinku Garg -
my i phone 5c wont stop restarting!! sometimes it takes hours before the phone starts working again. Sometimes when it does this red lines go up and down the screen. Im so ready to go back to my 4s does anyone know how to fix this??? Should i mention i have tried restarting back to new and i have the most up to date software. Please help
If the center portion of the monitor is flickering then your GPU on the logic is bad or (unlikely given this symptom) that your monitor cable is pinched/damaged.
**Imediately backup ALL your important data while you can still see things on the monitor
Contact Apple for appt. for in shop diagnostics for full examination and for parts replacement
nobody can give you a cost on a repair that isnt fully diagnosed online and cannot be.
perform SMC reset just to check:
Shut down the computer.
Plug in the MagSafe power adapter to a power source, connecting it to the Mac if its not already connected.
On the built-in keyboard, press the (left side) Shift-Control-Option keys and the power button at the same time.
Release all the keys and the power button at the same time.
Press the power button to turn on the computer. -
Creating new BAT file script to stop, restart and disable S I agent
We have just installed BOXI3 and are in the process of updating some bat files for stopping, restarting and failover (to our backup servers)and disable services on the Central Configuration Manager. In my old scripts we called each service Examples (
sc
servername.net config "bobjspcengine" start= disabled
sc
servername.net config "BOBJWebiServer" start= disabled)
I have a few questions
1. Server Intelligence Agent... Where would I find it's name like the BOBJWebiServer example?
2. If my bat file stops this agent does that automatically stop the CMS, Webi, Report services?
3. This is for our support team in order to easliy stop Tomcat and other services for failing over to our DR sites and backups since the script automatically stops, starts, restarts disables and enable services..., is this the best approach? We put these bat files out on the servers and all they do is click on them to start the failover process.Try using
Process Monitor for looking the key.
For example, you may set the required value through the group policy and see what windows registry keys are changed. -
Is root required to start/stop/restart mysql?
I'm trying to have all users in the mysql be able to start/stop/restart mysql. I'm currently getting permission denied for the /var/lib/mysql folder even though I'm logged in as a user that is a member of the mysql group (and mysql is the owner of the directory)
Is what I am trying to achieve possible?
I'm not sure if this is related but I am also unable to start mysql from rc.d on bootup, but once I am logged in I can use the root user to start it up.
Thank you!/etc/rc.d/mysqld wrote:/usr/bin/mysqld_safe --user=mysql &>/dev/null &
Doesn't mysqld get started as mysql user by default?
Did you modify any of mysql's configuration files while trying to do this? I imagine that would cause troubles for you.
Post anything else you think is relevant, too. -
How to start/stop/restart a java desktop application from remote machine
Hi,
I want to know is there a way in java where i can start/stop/restart a java desktop application running on a remote machine through another java desktop application?
For e.g i have an Admin console which monitors its clients based on socket communication, all of them are java desktop applications. I want to also give start/stop remote clients through my Admin console.
I am thinking in terms of windows system service which can start/restart/stop my clients on request of Admin console, but how can i call this system service remotely?
How can i do it?I got it. its about connection...
-
Instead of using myThread.stop()
I should do somthing like this ... correct?
//In my Thread Dispatcher Class
private boolean shouldStop = false;
private Thread myThread;
public static void setStopThread(boolean b){
shouldStop = b;
public boolean stopThread(){
return shouldStop;
public void nullifyThread(){
myThread = null;
//In my Thread Class
public void run(){
while(!myThreadDispatcherInstance.stopThread()){
//do some Thread Work
}//end while
}//end run method
myThreadDispatcherInstance.nullifyThread();Will the above code handle everything (aside for the setup and initialization part) to override the depreciation of the Thread.stop() method? Or am I missing somthing?
oh yea also anywhere I'd need to stop the thread i'd put
ThreadDispatcherClass.setStopThread(true);Looks about right, except that you need to declare setStopThead and stopThread synchronized, in order to force writing to and reading from main memory.
NullifyThread is almost certainly pointless and unneeded.
stopThread is a rather poor name. It sounds like it's taking an action, rather than returning a value. shouldStop() or idDone() or something would be better.
Finally regarding "In my Thread class": Rather than extending Thread, you should implement Runnable. You're not really specializing a Thread's behavior, you're just implementing a unit of work for a thread to run. -
Stopping a thread from running
Hai
I am new to java. It would be helpful for me if someone helps me out in doing this.
Actually in my program ,I should
i. Find out whether a particular file exists in the given directory or not.
ii. If the file doesn't exist, my program should wait for certain timeframe and check for the same file again.
iii. If the program cannot find the required file within the timeout specified, my application should exit.
I have done (i) and (ii) using threads. But in the case of (iii), I came to know that stop() method is depricated.
I am providing my code for a better understanding. Could you please let me know, what would be the best way to stop the thread from running after the timeout is reached in my program?
Thanx in advance
import java.io.File;
public class Fileexists implements Runnable
static Thread t;
public static void main(String[] args)
System.out.println("creation of object");
FileExists test = new FileExists();
t = new Thread(test);
t.start();
// run method
public void run()
File f = new File("searchfile.txt");
boolean b = f.exists();
System.out.println();
if(b == false)
System.out.println("Required file doesnot exists");
try
t.sleep(20000);
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
t.run();
else
System.out.println("Required file found");
} // end of run()
} // end of FileExists classActually your use of threads is a difference which makes no difference. The JVM starts an initial thread and runs your main(), at the end of which you start a thread and return. So you've just replaced one thread with another. There's no parallel processing.
The t.run() at the end of your run() method won't work. A particular Thread object can only run once. It will give you an IllegalStateException.
I can't see how multi-threading will help you with this task at all, all you need is a simple loop with a Thread.sleep in it.
If you really want to use a separate thread or Timer to implement a timeout (as an exercise) then use Thread.interrupt() to wind up a thread. The looping thread should test with Thread.interrupted() in it's loop condition, if it's in sleep() when interrupted an InterruptedException will be thrown which you should catch and allow to break the loop and let the thread terminate.
So your loop structure looks like:
try {
while(!Thread.interrupted()) {
... do your test
Thread.sleep(.....);
System.out.println("Thread terminated by detecting interrupted");
} catch(InterruptedException ex) {
System.out.println("Thread interrupted from sleep");
You can get the Thread for the main thread simply by storing Thread.currentThread() in a Thread reference.
I'd suggest using a java.util.Timer to launch your time-out and leave the loop on the main thread started by the JVM. -
Stopping a Thread (where I don't have control of run)
Is there any way to stop a thread whose run method simply calls a method of another class?
For example:
public class myThread extends Thread
private String className = ...;
public void run()
// get className's "main" method and run itActually...Smarsh is right. This is the correct way
to stop a thead....stop() is deprecated because it can
cause a deadlock condition. So, in run(), if you
defined a boolean like "bStopThread" run would look
like this:
public void run() {
if(bStopThread)
return;
// if we drop here...this is the rest of our run
un code
Yeah, I get that's how you should stop a thread...
Let me rephrase the question a little. Say the thread's run method looks similar to this:
public void run()
if (stopped)
return;
someOtherClass.aMethod();
}Once it's in aMethod, I have to let it run until it's done, right? There's no other way to stop a thread other than checking a stop condition? -
Stopping a Thread (no control on run method)
Hi,
How can we stop a Thread like in the following scenario. If we are in the aMethod() and the stopped variable is set to true by some other thread now how can we return from this run method and stop executing the aMethod(). Any tips are helpful.
public void run()
if (stopped)
return;
someOtherClass.aMethod();
ThanksI miss the scenario here, where a thread is blocked inside a call to some operating system resource (such as accept() or read() or write()).
In this case an InterruptedException travels like pacman up the stack, until it's caught, and I think that it should be a scenario in all cases, because it can apply to the blocking call (where it applies already), the predictable loop (try { while() { } } catch (InterruptedException ie) { }), and the one-off algorithm, even though in the latter case, if it's extremely important that we know exactly what we were doing when we were interrupted, it's difficult to avoid either a) many try-catch-blocks, or b) a very good way of examining the stack at the moment of interruption.
This is, in fact, so important, that I would like to urge Sun to change the API for java.lang.Runnable into
public interface Runnable {
public abstract void run() throws InterruptedException;
}Because, in this way, any thread will always have an endpoint. If you choose to rethrow the exception you still mess up the JVM, of course, like it was, causing it to exit. But at least you must provide yourself with the opportunity to let threads not end, or always die softly.
Maybe you are looking for
-
When I click to open a photo it opens a black screen with a grey triangle, exclamation mark in the middle. How do I fix this problem?
-
I'm buying a Mac mini in September - so I will have Snow Leopard - to use with the latest version of Logic and the latest version of Pro Tools and use for my college work and recording my band. I plan on using an M-Audio MobilePre USB, external hard
-
HP Photosmart Premium e-All-in-O​ne (CN503B) - ePrint - invalid code
I'm having difficulties adding the printer to my account,. I've tried the "disable, enable" web service trick, but still no luck... any suggestions?
-
Sap R/3 4.6c and BW 3.0b installation snapshot steps both on one server
hi gurus, i want to install R/3 4.6c and BW 3.0b on to my laptop my laptop configuration is 2bg RAM 160 HDD Intel centrino Duo mobile 1.66ghz processor i also need the connectivity steps to be working between R/3 and BW for me to extract data from R/
-
LSAlert Job References OLD Database
I'm using LogShipping and have it running on 2 databases just fine. The job that was created when I set up and was testing the LogShipping called LSAlert runs every 2 minutes and fails because it is referencing an OLD database that was used to set u