multithreading - Using Multiple Threads in Java -


i've been trying understand how multi-threading work ran below code:

/*  * change template, choose tools | templates  * , open template in editor.  */ package mainbowl;  import java.util.logging.level; import java.util.logging.logger;  /**  *  * @author kbluue  */ public class threadstudy {  thread t, t1, t2; runnable r, r1, r2;  public threadstudy() {     init(); }   public void start(){  }  private void init(){     t = new thread(r);     t.setname("thread");     t1 = new thread(r1);     t1.setname("thread 1");     t2 = new thread(r2);     t2.setname("thread 2");      r = new runnable() {          @override         public void run() {            // throw new unsupportedoperationexception("not supported yet.");             if (t != null){                 printstart(t);                 try {                     thread.currentthread().wait();                 } catch (exception e){                     printerror(t);                 }                 printresume(t);                 t1.notify();             }         }     };     r1 = new runnable() {          @override         public void run() {            // throw new unsupportedoperationexception("not supported yet.");             if (t != null){                 printstart(t1);                 try {                     t1.wait();                 } catch (interruptedexception ex) {                     logger.getlogger(threadstudy.class.getname()).log(level.severe, null, ex);                 }                 printresume(t1);                 t2.notify();             }         }     };     r2 = new runnable() {          @override         public void run() {            // throw new unsupportedoperationexception("not supported yet.");             if (t != null){                 printstart(t2);                 t.notify();                 printresume(t2);             }         }     }; }  private void printstart(thread t){     system.out.println("this new thread starting" + t.getname()); }  private void printresume(thread t){     system.out.println("this thread resuming" + t.getname()); } private void printerror(thread t){     system.out.println("there error" + t.getname()); }  public void run(){     t.start();     t1.start();     t2.start(); } 

}

and ran in main activity

new threadstudy().run; 

but output gave nothing. just;

run: build successful (total time: 0 seconds) 

not error message work with.

also i'd know if there more ways add commands/methods thread other new thread(runnable r) method. in anticipation.

it's worth reading here oracle java tutorial - defining , starting thread

an application creates instance of thread must provide code run in thread. there 2 ways this:

  • provide runnable object. runnable interface defines single method, run, meant contain code executed in thread. runnable object passed thread constructor.

  • subclass thread. thread class implements runnable, though run method nothing. application can subclass thread, providing own implementation of run.


you can without using runnable interface overriding default run() method of thread class

sample code:

t = new thread(){     @override     public void run() {        // add logic here        // or call method here     } }; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -