Can anyone introduce me a tutorial or sample about "interrupt" of Thread?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   George_Lin
Posted On:   Monday, March 1, 2004 05:12 AM

			
Hello, everyone!


I am learning JAVA Thread. I have read all the parts of section "Threads: Doing Two or More Tasks at Once (Essential Java Classes) " of the tutorial "The JAVA Toturial" on java.sun.com but failed to find anything about the usage of function "interrupt".

Can anyone introduce me some tutorials or simple source codes dealing with the usage of the function call? I just want to know the basic principle and usage of this function call.


Thanks in advance,
George

Apologies, Heres A Formatted Example

Posted By:   Mark_Stewart  
Posted On:   Saturday, March 27, 2004 11:10 AM


import java.util.*;

/**

 *

 * @author  Mark Stewart

 */

public class ThreadInterrupt {

    

    static Object lock=new Object();

    

    /** Creates a new instance of ThreadInterrupt */

    public ThreadInterrupt() {

    }

    

    public static void main(String[] args){

        Runnable first=new Runnable(){

           public void run(){

                System.out.println(Thread.currentThread().getName()+"-"+

                                    new Date().toString()+":I am starting now");

                try{

                    synchronized(lock){

                        lock.wait();

                    }

                }catch(InterruptedException ex){

                    System.out.println(Thread.currentThread().getName()+"-"+

                                        new Date().toString()+":I've been interrupted");

                }

            }

        };

        

        final Thread firstThread=new Thread(first);

        

        Runnable second=new Runnable(){

            public void run(){

                System.out.println(Thread.currentThread().getName()+"-"+

                            new Date().toString()+":I'm waiting for 5s before I iterrupt the first thread");

                try{

                    synchronized(lock){

                        lock.wait(5000);

                        System.out.println(Thread.currentThread().getName()+"-"+

                                    new Date().toString()+":I am interrupting the first thread now");

                        firstThread.interrupt();

                    }

                }catch(InterruptedException ex){}


            }

        };  

        

        final Thread secondThread=new Thread(second);

        // Keep the main thread running

        firstThread.setDaemon(false);

        secondThread.setDaemon(false);

        // Start the threads

        firstThread.start();

        secondThread.start();

        

    }

    

}

Re: Can anyone introduce me a tutorial or sample about "interrupt" of Thread?

Posted By:   Mark_Stewart  
Posted On:   Saturday, March 27, 2004 11:02 AM

This will show you how interrupt works

import java.util.*;
/**
*
* @author Mark Stewart
*/
public class ThreadInterrupt {

static Object lock=new Object();

/** Creates a new instance of ThreadInterrupt */
public ThreadInterrupt() {
}

public static void main(String[] args){
Runnable first=new Runnable(){
public void run(){
System.out.println(Thread.currentThread().getName()+"-"+
new Date().toString()+":I am starting now");
try{
synchronized(lock){
lock.wait();
}
}catch(InterruptedException ex){
System.out.println(Thread.currentThread().getName()+"-"+
new Date().toString()+":I've been interrupted");
}
}
};

final Thread firstThread=new Thread(first);

Runnable second=new Runnable(){
public void run(){
System.out.println(Thread.currentThread().getName()+"-"+
new Date().toString()+":I'm waiting for 5s before I iterrupt the first thread");
try{
synchronized(lock){
lock.wait(5000);
System.out.println(Thread.currentThread().getName()+"-"+
new Date().toString()+":I am interrupting the first thread now");
firstThread.interrupt();
}
}catch(InterruptedException ex){}

}
};

final Thread secondThread=new Thread(second);
// Keep the main thread running
firstThread.setDaemon(false);
secondThread.setDaemon(false);
// Start the threads
firstThread.start();
secondThread.start();

}

}
About | Sitemap | Contact