Saturday, 10 June 2017

Java Tutorial: Java Threads (Interrupting a Thread behaves normally | Java thread interrupt) ~ foundjava


Click here to watch in Youtube : 


https://www.youtube.com/watch?v=jI284uQU5p0&list=UUhwKlOVR041tngjerWxVccw

DisplayThread.java

/*
 * Example of interrupting thread that behaves normally.
 *  
 * If thread is not in sleeping or waiting state,
 * calling the interrupt() method sets the interrupted
 * flag to true that can be used to stop the thread by
 * the java programmer later.
 */
class DisplayThread extends Thread
{
    public void run()
    {
        for (int i = 1; i <= 10; i++)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        DisplayThread t1 = new DisplayThread();
        t1.start();

        t1.interrupt();
    }
}
Output
1
2
3
4
5
6
7
8
9
10
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment