Thread Implementation in Java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Talal_Masood
Posted On:   Thursday, August 10, 2006 07:55 PM

Hello,

I am layman to Java, and my professor gave me an assignment to work on Java Threads. I have to demonstrate 2 simple codes.

1- Write any program in which you can show Multi-Threading.
2- Write the same program but this time without Multi-threading.


So please if anyone can help me out please!
I have to submit my assignment by Monday morning got only couple of days left.


Thanks!

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.
About | Sitemap | Contact