This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gevater_Tod4711
Gevater_Tod4711 wrote ...

2013/8/29

Problem with interrupting a thread

Gevater_Tod4711 Gevater_Tod4711

2013/8/29

#
Hi all, I'm having some problems with a thread that I just can't interrupt. I'm using sleep in this method and when the sleep is interrupted I catch the InterruptedException that is thrown but the thread just runs on and is not interrupted. Has anyone an idea why the thread is not interrupted? Here's the code I used:
    private int activationTime;
    private int disactivationTime;
    
    private Thread pulseGeneratingThread;
    
    public PulseGenerator() {
        this(1000, 1000);
    }
    public PulseGenerator(int activationTime, int disactivateionTime) {
        this.activationTime = activationTime;
        this.disactivationTime = disactivationTime;
        for (int i = 0; i < inputLinks; i++) {
            inputLink.add(new InputLink(this));
        }
        pulseGeneratingThread = new Thread(new PulseGeneratorThread(), "PulseGeneratorThread");
        outputLink = new OutputLink(this);
        setImage(createImage());
    }
    
    class PulseGeneratorThread implements Runnable {
        
        public void run() {
            while(true) {
                setActivated(true);
                try {
                    Thread.currentThread().sleep(activationTime);
                }
                catch (InterruptedException ie) {
                    //ie.printStackTrace();
                }
                setActivated(false);
                try {
                    Thread.currentThread().sleep(activationTime);
                }
                catch (InterruptedException ie) {
                    //ie.printStackTrace();
                }
            }
        }
    }
    
    protected void firePositiveEdge(InputLink link) {
        pulseGeneratingThread.start();
    }
    
    protected void fireNegativeEdge(InputLink link) {
        pulseGeneratingThread.interrupt();
        setActivated(false);
    }
With the firePositiveEdge(InputLink) method it is started (which works quite well). When the method fireNegativeEdge(InputLink) is executed the thread should be interrupted but it isn't. It just runs on and activates or disactivates the object till I end the execution of my simulation. If anyone has an idea what I'm doing wrong that would be gread. Thanks guys.
Game/maniac Game/maniac

2013/8/29

#
Try breaking the loop
Gevater_Tod4711 Gevater_Tod4711

2013/8/29

#
I already tried breaking the loop. The Thread was stoped but I couldn't activate it again. I now fixed the problem by starting a new Thread. But it would be still interesting to me if there is a better way or what the hell I am doing wrong. However thanks for the help Game/maniac.
SPower SPower

2013/8/29

#
@Game/maniac In that case the tread would stop completely. @Gevater_Todd, I advice something like this:
private boolean sleeping = false;
private long sleepStartTime;

// at the beginning of your loop:
if (sleeping) {
    if (sleepStartTime+activationTime >= System.currentTimeMillis()) {
        sleeping = false;
    } else continue;
}

// in your catch statements:
sleeping = true;
sleepStartTime = System.currentTimeMillis();
davmac davmac

2013/8/29

#
Gevater_Tod4711, the reason the thread keeps running is precisely because you catch the interrupted exception and then do not terminate the thread. There's nothing in the exception handler (the 'catch' block) so once that's done the loop continues. I think you want:
    class PulseGeneratorThread implements Runnable {  
        public void run() {  
            try {  
                while(true) {  
                    setActivated(true);  
                    Thread.currentThread().sleep(activationTime);  
                    setActivated(false);  
                    Thread.currentThread().sleep(activationTime);  
                }  
            }  
            catch (InterruptedException ie) {  
                //ie.printStackTrace();  
            }  
        }  
    }  
See that the 'catch' block is outside the while loop now.
Gevater_Tod4711 Gevater_Tod4711

2013/8/29

#
@davmac Ok I see. Thanks for the help guys.
You need to login to post a reply.