Results 1 to 7 of 7

Thread: interrupting threads

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    interrupting threads

    The documentation is confusing me on this.

    What I want is having a thread sleep until I give it a GO signal. How would I do this? I have looked into sleep() and interrupt(), but I don't really understand it. What does the "interrupted" state mean? Is it related to the running state of the thread? If yes, how? If no, how do I get the running state?

    I want to be able to stop the thread again with another signal. How would I do that? suspend, stop etc. are deprecated.

    Thanks in advance
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I now use a signaling model with Object.wait() and Object.notify(), but I don't undestand their documentation either, so it might not work. Especially the parts about monitor ownership.

    On a related note, is there something like a critical section in Java? I have several threads, and most of them might want to access one global java.awt.Robot object, I have to synchronize the access.

    Here's the signaling code I currently use:
    Code:
    class SomeClass implements Runnable
    {
    	private Object startSignal;
    	private boolean running;
    	private Thread solver;
    
    	public SomeClass()
    	{
    		startSignal = new Object();
    		solver = new Thread(this);
    		setRunning(false);
    		solver.start();
    	}
    
    	public void startstop()
    	{
    		if(isRunning()) {
    			setRunning(false);
    		} else {
    			setRunning(true);
    			startSignal.notify();
    		}
    	}
    
    	synchronized boolean isRunning()
    	{
    		return running;
    	}
    
    	synchronized void setRunning(boolean r)
    	{
    		running = r;
    	}
    
    	// this might cause huge synchronization problems...
    	public void run()
    	{
    		while(true) {
    			startSignal.wait();
    			while(isRunning()) {
    				internalStep();
    			}
    		}
    	}
    }
    internalStep does calculations.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem is that there might (and will) be several objects of SomeClass, each having it's own thread trying to access the same java.awt.Robot
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    http://java.sun.com/docs/books/tutor...o/threads.html

    Any use? Theres some stuff on synchronising blocks etc.

    HD

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    From what i remember of multithreadding the sleep method is used to put the thread to sleep for a period of time specified by the arguement passed to the method.

    Calling wait() causes the current thread to block until the objects notify() method is called by another thread.

  6. #6

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Dave: seems interesting, thanks.

    Dilenger: yeah, that's about the part of the docs I understood . Nothing else.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Dave: just found out it's exactly what I needed. Double thanks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width