java - confusion regarding multithreading -


i beginner in java field , going through multithreading concepts

i came across program , had doubts regarding program itself.

following program

public class secondthreaddemo implements runnable {     public void run() {         system.out.print(thread.currentthread().getname());     } }  class b implements runnable {     public void run() {         new secondthreaddemo().run();         new secondthreaddemo().run();         new thread(new secondthreaddemo(), "t3").run();         new thread(new secondthreaddemo(), "t2").start();         new secondthreaddemo().run();         new thread(new secondthreaddemo(), "t3").start();     } }  class c {     public static void main(string[] args) {         new thread(new b(), "t1").start();     } }.. 

now expecting following output

t1t1t1t1t3t3 

but coming

t1t1t1t1t2t3 

can clarify output?

there's difference between run , start.

runnable isn't thread execute inline rest of code (like calling other method).

start schedules thread can run of runnable, in example.

there differences each time run , come down how thread scheduling working

for example, code this...

new secondthreaddemo().run(); new secondthreaddemo().run(); new thread(new secondthreaddemo(), "t3").run(); new thread(new secondthreaddemo(), "t2").start(); new secondthreaddemo().run(); new thread(new secondthreaddemo(), "t3").start(); 

this (possibly) output like...

t1 (run) t1 (run) t1 (run) (cause i'm in t1's thread context) // depends...this might output... {t1} (run) {t2} (start) {t3} (start) 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -