84 How to develop user define thread in Java?

By Directly Writing Logic in Run Method

Code :

class MyThread extends Thread
{
@Override
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(" Run: "+Thread.currentThread().getName() );
}
}

public static void main( String args[] )
{
MyThread mt = new MyThread();

mt.setName("CodingAtharva");
mt.start();

for(int i=1;i<=10;i++)
{
System.out.println(" Main: "+Thread.currentThread().getName() );
}

}
}




Output:



By InDirect Writing Logic in Another Method

Code :

class Test extends Thread
{
@Override
public void run()
{
System.out.println("run: "+ Thread.currentThread().getName() );
this.m1();
}

public void m1()
{
System.out.println("m1: "+ Thread.currentThread().getName() );
}

public static void main( String args[] )
{
System.out.println("main: "+ Thread.currentThread().getName() );
Test t =new Test();
// t.m1();
t.setName("CodingAtharva");
t.start();
}
}




Output:


Previous
Next Post »