0% found this document useful (0 votes)
8 views

Java_Labprogram_11

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java_Labprogram_11

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to illustrate creation of threads using runnable class.

(start method start each

of the newly created thread. Inside the run method there is sleep() for suspend the thread for 500

milliseconds)

Save Filename as: ThreadMain.java

Solution:-

class MyRunnable implements Runnable

public void run()

try

System.out.println("Thread " + Thread.currentThread().getId() + " is running.");

Thread.sleep(500);

catch (InterruptedException e)

System.out.println("Thread interrupted: " + e.getMessage());

public class ThreadMain

public static void main(String[] args)

int numThreads = 5;

for (int i = 0; i<numThreads; i++)


{

Thread thread = new Thread(new MyRunnable());

thread.start();

Compile As: javac ThreadMain.java

Run As: java ThreadMain

Output:

Thread 10 is running.

Thread 12 is running.

Thread 14 is running.

Thread 13 is running.

Thread 11 is running.

You might also like