//Multithreading in Java
Description
Multi threading in Java allows multiple process to run simultaneously. It can be implemented in two ways, by extending the Thread class or imlementing the Runnable interface. The run() method is used to implement multi threading. The sleep() method pauses the execution of the current thread for the specified time. The start() method is used to start the thread by calling the run() method.
Program
class Multi extends Thread { int i=0,delay; String word; Multi(String word,int delay) { this.word=word; this.delay=delay; } public void run() { try { while(i<5) { System.out.println(word); i++; Thread.sleep(delay); } } catch(InterruptedException e) { return; } } public static void main(String s[]) throws Exception { Multi m1=new Multi("S1",100); m1.start(); Multi m2=new Multi("S2",100); m2.start(); } }
Output
Multithreading in Java |
No comments:
Post a Comment