Programmer's Blog

Programmer's reference

[Java] Simple Multithreading

An example code is as follows

class MyThread implements Runnable {

 Thread thrd;

 MyThread(String name) {   //constructor
 thrd = new Thread(this, name);
 thrd.start();
 }

 public void run() {

 try {
   do {
       Thread.sleep(500);
       System.out.println(thrd.getName()); //get threadname

      } while (1);
     } catch(InterruptedException e) {
     System.out.println(thrd.getName() + " interrupted.");
 }
 }

}//class MyThread

public class Test {
 public static void main(String args[]) {

        MyThread mt = new MyThread("Child #1");  //run obj in thrd

 } //main
} //class Test

Advertisements

One response to “[Java] Simple Multithreading

  1. Johnb522 May 13, 2014 at 10:56 pm

    An interesting dialogue is worth comment. I feel that you must write more on this topic, it won’t be a taboo subject but typically people are not sufficient to speak on such topics. To the next. Cheers kggeedcdgdfc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: