A thread is a light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads.The process of executing multiple threads simultaneously is known as Multithreading.
Code: Implement Thread using extend Thread Class.
class Thread1 extends Thread
{
public void run()
{
System.out.println("Pritpal");
}
public static void main(String[]args)
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}
Result:
class Rthread implements Runnable
{
public void run()
{
System.out.println("Thread is running");
}
public static void main(String[]args)
{
Rthread m1=new Rthread();
Thread t1=new Thread(m1);
Thread t2=new Thread(m1);
t1.start();
t2.start();
}
}
Result:
class Thread1 extends Thread
{
public void run()
{
System.out.println("Pritpal");
}
public static void main(String[]args)
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}
Result:
Code: Implement Thread using implement Runnable Interface.
{
public void run()
{
System.out.println("Thread is running");
}
public static void main(String[]args)
{
Rthread m1=new Rthread();
Thread t1=new Thread(m1);
Thread t2=new Thread(m1);
t1.start();
t2.start();
}
}
Result:
0 Comments