AIM: Write a program that executes two threads. One thread displays “Thread1” every 2,000 milliseconds, and the other displays “Thread2” every 4,000 milliseconds. Create the threads by extending the Thread class.
TwoThread.java
Output
Happy Coding :)
TwoThread.java
class ThreadExample extends Thread { ThreadExample(String s) { super(s); start(); } public void run() { for(int i=0;i<5;i++) { System.out.println(Thread.currentThread().getName()); try { if(Thread.currentThread().getName()=="Thread1") { Thread.sleep(2000); } else { Thread.sleep(4000); } } catch(Exception e){} } } } class TwoThread { public static void main(String arg[]) { System.out.println("Thread name : "+Thread.currentThread().getName()); ThreadExample e1=new ThreadExample("Thread1"); ThreadExample e2=new ThreadExample("Thread2"); } }
Output
run two thread in java by practical server |
Happy Coding :)
0 Comments
Post a Comment