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).
Note:
PROGRAM:
Code
1: ThreadExample class
@Override
public
void run() {
try
{
System.out.println("Thread
" + Thread.currentThread().getId()
+ " is running.");
Thread.sleep(500);
System.out.println("Thread
" + Thread.currentThread().getId()
+ " is awake.");
} catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
Code
2: ThreadExample class
public
static void
main(String[] args)
{
int
numberOfThreads = 5;
Thread thread
= new Thread(new
MyRunnable());
thread.start();
}
}
}
No comments:
Post a Comment