Start/Stop a thread dynamically.

i have an attribute Thread t;
i initialize it as
t = new Thread(){
     public void run(){
          try{
               System.out.println("do something");
               sleep(10000);  // sleep a while
          catch(Exeception e){}
};i invoke it by t.start(); and stop it by t.sopt(), but i failed in t.start() to start it again
How could i start and stop it externally without re-new the object ?

The stop() method should not be used.
Instead you might want to try the pattern:
volatile Thread aThread=null;
public void run() {
aThread = this.currentThread();
while(aThread!=null) {
//sleep
and write a method to set aThread to null to stop the thread.

Similar Messages

  • How do I select multiple albums in Revel to start/stop sharing?

    How do I select multiple albums in Revel to start/stop sharing?

    Hello,
    You have to select each one to share the album. You can have multiple albums sharing at the same time. Please see our FAQ post "How to share photos with Revel"
    http://forums.adobe.com/thread/1157995?tstart=0
    For additional helpful information regarding Revel , please see our main FAQ Forum http://forums.adobe.com/community/ps.com_sharing_and_storage
    Thanks
    Scott

  • I have installed Premiere Elements 11 from a disc I bought from Amazon.  I keeps asking me to re-install or if it does start stops a few minutes after I start a project.  I have a windows 8.1 PC.  I have tried numerous times to install the program and als

    I have installed Premiere Elements 11 from a disc I bought from Amazon.  I keeps asking me to re-install or if it does start stops a few minutes after I start a project.  I have a windows 8.1 PC.  I have tried numerous times to install the program and also to open it as Administrator as suggested by a friend but it still refuses to work.  I need direct help from Adobe but don't know how to get to them on this website as they seem to rely on user forums to advise customers.

    WhiteHouse Norm
    This question is already being responded to in the following duplicate thread
    Premier elements 11 keeps stopping
    Please reply in the other thread which has a discussion in progress.
    Thank you.
    ATR

  • How to stop the thread?

    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 Garg

    Hi,
    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

  • Restart/start/stop network from command line

    Hi all...
    How do I restart/start or stop my network from command line? I red somewhere about using netstart, but I guess the article was refering to OpenBSD and not OS X...
    So.. any hints?
    Thanks.

    Hey You are right there...
    OKK..To my knowledge, you can start an application, from the servlet in a thread. But stopping a thread from outside is not safe and even sun doesn't advice.
    1. Starting a thread : you can start your program using an Runtime class. But your client program can see if the server port is open or not. So after starting the server program, through servlet, execute the client program. OR you can log every command/step in to a log file, and start the server program and see the log, where currently the program is waiting, so you can check, if the server program has exited.
    2. Stoping an application i.e a thread is a tricky thing. One i would suggest is, send data to the server socket with your pass code and the command "Shutdown" with a sequence which will not repeat in the data communication. If the first command is your passcode and "Shutdown" you can kill the server thread from inside the server application. To make this possible, every thing the new connection is accepted, you have to validate the first line is this your command to shutdown.
    and repeat the step 1.
    Hope this gives you a little idea to proceed. if not let me know...
    Cheers
    Venkat

  • How to start, stop and restart task

    Hello Everyone,
    Need advice on what is the correct way to start, stop and restart a Java task?
    I have tried with Executors.newCachedThreadPool() or fixedThreadPool() however I was only able to submit the task once. My program crashed when i tried to resubmit the task again after a shutdownnow command()

    Tasks are designed to be run once. If you want restart functionality, use a service:
    http://docs.oracle.com/javafx/2/api/javafx/concurrent/Service.html
    The sample service I posted in the following thread demonstrates how to do this:
    Multithreading
    Use of Executors is not necessary as a Service encapsulates the execution thread management (though you can set your own executor on the service if you really needed it). All you need to do is make use of the service start, cancel, restart and reset methods as required. If you do use the cancel method, then you need to make sure that your service is properly interrupt aware for the cancel to function as you expect.

  • 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 class

    Actually 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.

  • DV Start/Stop detect with iMovie

    http://discussions.apple.com/thread.jspa?forumID=936&threadID=557928
    Because this thread was marked solved I can't add to it.
    I think the problem is the rendering as you surmised. There is no way to fix this problem because of the way iMovie works. It captures without timecode so every clip begins at zero. Once you've rendered the material and moved it to the browser the render file looks to the TC on the clip to sync up. Once it's broken up every subclip starts with zero because it has no inherent timecode so the render file associated with zero TC links to every subclip.

    Hi Tom,
    thanks for addressing that topic again (I had just marked it "solved" because it was open by a long time...).
    I see what you mean: since there is no TC in the .dv from iMovie there is no way to sync the audio render file and the subclips. Another interpretation is that DV Start/Stop Detect applies only to the original .dv media, not to audio (item level) render file created in the timeline, so FCE cannot associate different sections of the audio render file to the corresponding subclips.
    But now that I read it again... I think we are just saying the same thing!
    And you are suggesting a feedback would be useless.
    Thanks anyway
    Piero

  • Starting/Stopping 9iAS Infrastructures with batch files

    I created the following two batch files to start and stop my iAS Infrastructure. I found that if I used the services settings of automatic, all did not go well.
    start.bat:
    @ECHO OFF
    cls
    ECHO About to start the listener
    net start Oracle9ias_InfraTNSListener
    ECHO About to start the database
    net start OracleServiceIASDB
    ECHO About to start the OID monitor
    e:\ora9iasinfra\bin\oidmon start
    ECHO About to start the OID server
    e:\ora9iasinfra\bin\oidctl server=oidldapd configset=0 instance=1 start
    ECHO About to start the website
    net start Oracle9ias_infraEMWebsite
    ECHO About to start opmn and OC4j
    e:\ora9iasinfra\dcm\bin\dcmctl start -ct ohs
    e:\ora9iasinfra\dcm\bin\dcmctl start -co OC4J_DAS
    stop.bat:
    @echo off
    cls
    ECHO About to stop the website
    net stop Oracle9ias_infraEMWebsite
    ECHO About to stop opmn and OC4j
    e:\ora9iasinfra\dcm\bin\dcmctl shutdown
    ECHO About to stop the OID server
    e:\ora9iasinfra\bin\oidctl server=oidldapd configset=0 instance=1 stop
    ECHO About to stop the OID monitor
    e:\ora9iasinfra\bin\oidmon stop
    ECHO About to stop the database
    net stop OracleServiceIASDB
    ECHO About to stop the listener
    net stop Oracle9ias_InfraTNSListener
    When I execute the batch files sometimes they work and sometimes they don't, for example after starting, occasionally the Apache process has not been created, and sometimes processes won't stop.
    However if I enter each command seperatly all works OK. Can anyone suggest a reason for this, maybe ordering is significant or a time delay may be required between certain actions?. Also after starting the website manager the process managers for both the infra and ias instances are started (I have installed both the iAS and the Infrastucture on the same machine - this all works fine when started as a series of individual commands but not when through batch files).
    Also If I try to use:
    SQLPLUS /NOLOG
    SQL>CONNECT sys/password@iasdb as sysdba
    SQL>Start
    I also get an 'unable to create a dedicated server' error. I can start/stop the database through the enterprise manager in either standalone or management server configuration without any errors. Can anyone shed any light on this?. I have set both TNS_ADMIN and ORACLE_SID correctly.
    Terry Bennett

    Thanks, I had read that and it was very informative. I did however solve my problem using a few other threads that have been posted about the runtime.runtime().exec commands. Thanks for you help.

  • Start/Stop Buttons and infinite loop exit

    I am trying to make a GUI with a Start/Stop and an Exit button. Initially the button will have the label "Start". When i push it, its label should become "Stop" and an infinite loop function will begin. I want the loop to run until i press the Stop or Exit button.
    The problem is that when the loop starts i can't press neither of the buttons. The "Start" button changes its label into "Stop" only if i make the loop finite and it ends.
    Here is the source:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class StartStopButtons extends JFrame{
        Component visualComponent = null;
        JPanel panel = null;
        JLabel statusBar = null;
         public StartStopButtons() {
              setSize(160, 70);
              getContentPane().setLayout(new BorderLayout());
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
            getContentPane().add(panel, BorderLayout.CENTER);
            final JPanel panel_1 = new JPanel();
            panel.add(panel_1, BorderLayout.CENTER);
            final JButton startButton = new JButton();
            startButton.addActionListener(new ActionListener() {
                 public void actionPerformed(final ActionEvent e) {
                    String action = e.getActionCommand();
                    if (action.equals("Start")) {
                         System.out.println("Start Loop");
                         startButton.setText("Stop");
                         myLoop ();
                    if (action.equals("Stop")) {
                         System.out.println("Stop Loop");
                         System.exit(0);
            startButton.setText("Start");
            panel_1.add(startButton);
            final JButton exitButton = new JButton();
            exitButton.addActionListener(new ActionListener() {
                 public void actionPerformed(final ActionEvent e) {
                    String action = e.getActionCommand();
                    if (action.equals("Exit")) {
                        System.exit(0);
            panel_1.add(exitButton);
            exitButton.setText("Exit");
         public void myLoop() {
              for (int i = 0; ; i++)
                   System.out.println(i);
         public static void main(String[] args) {
              StartStopButtons ssB = new StartStopButtons();
              ssB.setVisible(true);
    }

    I works just fine. Here is the source and thanks for the help.
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    public class StartStopButtons extends JFrame implements ActionListener, Runnable{
        Component visualComponent = null;
        JPanel panel = null;
        JLabel statusBar = null;
        Thread thread;
        JButton startButton;
         public StartStopButtons() {
            try {
                UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
            } catch(Exception e) {}
              setSize(160, 70);
              getContentPane().setLayout(new BorderLayout());
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
            getContentPane().add(panel, BorderLayout.CENTER);
            final JPanel panel_1 = new JPanel();
            panel.add(panel_1, BorderLayout.CENTER);
            startButton = new JButton();
            startButton.addActionListener(this);
            startButton.setText("Start");
            panel_1.add(startButton);
            final JButton exitButton = new JButton();
            exitButton.addActionListener(new ActionListener() {
                 public void actionPerformed(final ActionEvent e) {
                    String action = e.getActionCommand();
                    if (action.equals("Exit")) {
                        System.exit(0);
            panel_1.add(exitButton);
            exitButton.setText("Exit");
         public void actionPerformed(ActionEvent e) {
              String action = e.getActionCommand();
              if (action.equals("Start")) {
                   startButton.setText("Stop");
                   thread = new Thread( this );
                   thread.start();
              if (action.equals("Stop")) {
                System.exit(0);
         public void run() {
              myLoop();
         public void myLoop() {
              for (int i = 0; ; i++)
                   System.out.println(i);
         public static void main(String[] args) {
              StartStopButtons ssB = new StartStopButtons();
              ssB.setVisible(true);
    }

  • How to stop a thread within a thread.

    I have two threads. A progress Bar thread and it starts yet another thread.
    In the exception handler of the second thread how do I stop both the threads. thread.interrupt() seems to have to effect.
    Thanks,
    Veena

    jdevel doesn't let me debug;
    public class ProgressBarModel extends BoundedRangeModel {
    BooleanValueWrapper stopFlag = new BooleanValueWrapper(false);
    BooleanValueWrapper stopFlag2 = new BooleanValueWrapper(false);
    IntValueWrapper value = new IntValueWrapper(0);
    Thread newProgress = null;
    Thread newProcessFile = null;
    public void start(ActionEvent e) {
    value.setValue(0);
    stopFlag.setValue(false);
    ProgressSimulator progressSimulator = new ProgressSimulator();
    newProgress = new Thread(progressSumulator);
    newProgress.start();
    ProcessFileThreadMe processFileThreadMe = new ProcessFileThreadMe();
    newProcessFile = new Thread(processFileThreadMe);
    newProcessFile.start();
    class ProgressUpdater implements Runnable
    public void run(){
    try
    System.out.println(" ProgressUpdater is running");
    //stop fag is true if it is set to true or if value is equals or greater
    //than maximum
    try { 
    stopFlag.setValue(stopFlag.getValue() == true? true : (value.getValue() < maximum? false:true));
    //run in loop until stop condition is met. Make sure system doesn't
    //fail if values are initially set to the same value
    while (!stopFlag.getValue() && value.getValue() != maximum) {
    newProgress.interrupt();
    newProgress = null;
    } catch(Exception e) {
    stopFlag.setValue(true);
    stopFlag2.setValue(true);
    class ProcessFileThreadMe implements Runnable
    public void run(){
    System.out.println(" ProcessFileThreadMe is running");
    try
    if(stopFlag2.getValue() == true) {
    // interrupt this thread
    // set it to null
    return;
    bean.persisit(value, stopFlag);
    newProcessFile.interrupt();
    newProcessFile = null;
    catch (Exception exc)
    stopFlag.setValue(true);
    stopFlag2.setValue(true);
    exc.printStackTrace();
    In the bean the v alue is incremented
    Edited by: user597294 on Jul 21, 2011 6:33 AM

  • Stopping a Thread.... again

    Hi all,
    a couple of months ago I've posted a similar topic, on how to stop a thread without using the deprecated stop() method. Still, no result. I have a great deal of calculations that should be done in a separate thread. Due to the fact that the calculations might take up to 10 secons (or more), I would like to give the ability to the user to stop the process of calculations.
    public class Calculations(){
        //Field Declaration
          Thread t;
        public void doTheJob(){
            if(t != null)
                 t = null;
            t  = new Thread( new Runnable(){ public void run(){
                <PERFORM THE CALCULATIONS>
            t.start();
        public void stopTheJob(){
                 <HOW????, t.stop() works "fine">
    }The stop() method of class Thread will do perfectly and stop the procedure. However is deprecated. I've checked the alternative of stop using while( t!= null) loops, but there is no loop in the procedure!! There is just a bunch of operations that should be done in a separate thread. It is logical that something as the following won't do if the procedure has already started.
        public void stopTheJob(){
                 if( t != null)
                      t = null; //No point if the procedure has already started
        }Please correct me if I'm wrong, but help me understand how...
    I am not a programmer and not that smart but I still want to solve this issue.
    I would gladly provide more information if necessary.
    Thnx in advance,
    F.

    There are fundamental problems with monitors (i.e. synchronized bits). You can be sure they've given a lot of thought to cleaning up and decided there's no safe way to do it.
    stop still works and there are very rare situations in which I'd use it. In particular if the thread were running someone else's not fully trusted code and a framework decided an infinite loop was happening. Depracation is a warning, not an error.
    However it's generally no problem at all to have a thread watch for being interrupted and stop itself.
    Generally the interrupt call is the best mechanism since it kicks the thread out of wait or sleep, or sets a flag which you can test (or which will cause an exception next time you wait or sleep.

  • Start/Stop Channels Externally

    Hi,
    I have been looking at the blog /people/william.li/blog/2006/04/18/dynamic-configuration-of-some-communication-channel-parameters-using-message-mapping to see how this is done. I can see from the blog that a PI interface has been developed.
    I have ran the URL in a browser and get a 'Website cannot be displayed' error. I do not know what the cause of this is, whether the browser cannot parse the response, or the webservice is not called correctly. Could this problem be occuring if I do not have the relevant auth.
    Is it possible to execute the URL directly from ABAP. Has anyone been able to do it. I presume this webservice does not have to be calledd via PI.
    Is there a blog or anything other document which describes how this interface (with dynamic URL recevier) is configured in PI. I have searched and cannot find anything.
    Any help is greatly appreciated.

    Hi,
    If your requirement is to Start/ Stop XI channels externally, do the following:
    1. Select the specific channel in communication channel monitoring.
    2. Select External Control On for this channel.
    3. Use the following URL for accessing the channel based on what operation you need to perform:
        a) START: http://<host>:<port>/AdapterFramework/ChannelAdminServlet?party=<Party Name>&service=<Service Name>&channel=<Channel Name>&action=start
       b) STOP: http://<host>:<port>/AdapterFramework/ChannelAdminServlet?party=<Party Name>&service=<Service Name>&channel=<Channel Name>&action=stop
      c) STATUS: http://<host>:<port>/AdapterFramework/ChannelAdminServlet?party=<Party Name>&service=<Service Name>&channel=<Channel Name>&action=status
    (Keep Party Name blank if there is no party involved in the scenario).
    If you have a different requirement, I would request you to explain what you are looking for in more detail.
    Best Regards,
    Pratik

Maybe you are looking for