Re: Thread Implementation in Java
Posted By:
Anonymous
Posted On:
Friday, August 11, 2006 04:03 AM
Hi Dear,
This is not a big problem.
/**
*
* @author Avanish Kishor Pandey
*/
public class WithThread implements Runnable{
/** Creates a new instance of WithThread */
public WithThread(String threadName) {
Thread myThread = new Thread(this , threadName);
myThread.start();
}
public void run() {
System.out.println("Thread Name :::: "+ Thread.currentThread().getName());
for(int i=0 ; i< 10 ; i++){
try {
Thread.currentThread().sleep(100);
System.out.println("Hello Friend");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args){
System.out.println("Thread Name ::::" + Thread.currentThread().getName());
WithThread demoThread = new WithThread("Avanish");
System.out.println("End Of Main Method");
}
}
This is an example with two Thread one is main thread and other one userdefined Thread with name Avanish.
/**
*
* @author Avanish Kishor Pandey
*/
public class WithoutThread {
/** Creates a new instance of WithThread */
public WithThread() {
this.run();
}
public void run() {
System.out.println("Thread Name :::: "+ Thread.currentThread().getName());
for(int i=0 ; i< 10 ; i++){
System.out.println("Hello Friend");
}
}
public static void main(String[] args){
System.out.println("Thread Name ::::" + Thread.currentThread().getName());
WithoutThread demoThread = new WithoutThread();
System.out.println("End Of Main Method");
}
}
This is an example where multithreading is not used.