1.

How To Implement Threads In Java?

Answer»

Threads can be created in two ways i.e. by IMPLEMENTING java.lang.RUNNABLE interface or extending java.lang.Thread class and then extending run method.

Thread has its own variables and methods, it lives and dies on the heap. But a thread of execution is an INDIVIDUAL process that has its own call STACK. Thread are lightweight process in java.
1.Thread creation by implementingjava.lang.Runnableinterface.

We will create object of class which implements Runnable interface :

MyRunnable runnable=new MyRunnable();

Thread thread=new Thread(runnable);

2.And then create Thread object by calling constructor and passing REFERENCE of Runnable interface i.e. runnable object :

Thread thread=new Thread(runnable);

Threads can be created in two ways i.e. by implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.

Thread has its own variables and methods, it lives and dies on the heap. But a thread of execution is an individual process that has its own call stack. Thread are lightweight process in java.
1.Thread creation by implementingjava.lang.Runnableinterface.

We will create object of class which implements Runnable interface :

MyRunnable runnable=new MyRunnable();

Thread thread=new Thread(runnable);

2.And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object :

Thread thread=new Thread(runnable);



Discussion

No Comment Found