How to run multiple threads?

The main objective of multi threading is to run multiple threads simultaniously. It can be done like this:
class demo1 implements Runnable
public void run(){
for(int i=0;i<4;i++){
class demo
public static void main(String[] args){
demo1 obj=new demo1();
Thread t1=new Thread(obj,"first");
Thread t2=new Thread(obj,"second");
Thread t3=new Thread(obj,"third");
Thread t4=new Thread(obj,"fourth");
You can not guess the output.
It might be different in different times.
there is nothing in the Java specification that says threads will start
running in the order in which they were started .
Nothing is guaranteed in the preceding
code except this:
Each thread will start, and each thread will run to completion.
Within each thread, things will happen in a predictable order. But the actions
of different threads can mix together in unpredictable ways.

No comments:

Post a Comment