Multi Threading (1)
Multi Threading (1)
Java.lang.Thread.activeCount():-
This method is used to find out the number of methods in active state.
Public static int activeCount();
Ex:-
class MyThread extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
};
class ThreadDemo
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
t1.start();
t2.start();
t3.start();
System.out.println(Thread.activeCount());//4
}
};
Java.lang.currentThread():-
This method is used to represent current thread class object.
Public static thread currentThread()
Ex:-
class MyThread extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
};
class ThreadDemo
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
}
};
Java.lang.Thread.getId():-
getId() is used to generate id value for each and every thread.
Public long getId()
Ex:-
class MyThread extends Thread
{
public void run()
{
System.out.println(" rattaiah thread is running ");
}
};
class ThreadDemo
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
System.out.println("the thread id :"+t1.getId());
System.out.println("the thread name is :"+t1.getName());
System.out.println("the thread priority is "+t1.getPriority());
System.out.println("the thread id :"+t2.getId());
System.out.println("the thread name is :"+t2.getName());
System.out.println("the thread priority is "+t2.getPriority());
}
};
Interrupted():-
A thread can interrupt another sleeping or waiting thread.
For this Thread class defines interrupt() method.
Public void interrupt()
Effect of interrupt() method call:-
class MyThread extends Thread
{
public void run()
{
try
{
for (int i=0;i<10;i++ )
{
System.out.println("i am sleeping ");
Thread.sleep(5000);
}
}
catch (InterruptedException ie)
{
System.out.println("i got interupted by interrupt() call");
}
}
};
class ThreadDemo
{
public static void main(String[] args)
{
MyThread t=new MyThread();
t.start();
t.interrupt();
}
};
No effect of interrupt() call:-
class MyThread extends Thread
{
public void run()
{
for (int i=0;i<10;i++ )
{
System.out.println("i am sleeping ");
}
}
};
class ThreadDemo
{
public static void main(String[] args)
{
MyThread t=new MyThread();
t.start();
t.interrupt();
}
};
NOTE:-
The interrupt() is working good whenever our thread enters into waiting state or
sleeping state.
The interrupted call will be wasted if our thread doesn’t enters into the waiting/sleeping
state.
class MyThread extends Thread
{
public void run()
{
for (int i=0;i<10;i++)
{
Thread.sleep(2000);
System.out.println("CSE");
}
}
};
class ThreadDemo
{
public static void main(String[] args)throws Exception
{
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
t1.start();
t2.start();
t3.start();//4-threads
t1.join();
System.out.println(t1.getName());//thread-0
System.out.println(t2.getName());
System.out.println(t3.getName());
t1.setName("sneha");
System.out.println(t1.getName());//sneha
System.out.println(Thread.currentThread().getName());//main
Thread.currentThread().setName("poornima");
System.out.println(Thread.currentThread().getName());//poornima
System.out.println(Thread.activeCount());
System.out.println(t1.isAlive());
System.out.println(t1.getId());
System.out.println(t2.getId());
System.out.println(Thread.currentThread().getPriority());
System.out.println(t1.getPriority());
Thread.currentThread().setPriority(10);
System.out.println(Thread.currentThread().getPriority());
for (int i=0;i<5;i++)
{
Thread.sleep(5000);
Thread.yield();
System.out.println("main thread");
}
}
};
Difference between Process and Thread
The following table highlights the major differences between a process and a thread −
Comparison
Process Thread
Basis
Context Processes require more time for Threads require less time for
switching time context switching as they are context switching as they are
heavier. lighter than processes.
Memory Sharing Processes are totally A thread may share some memory
independent and don’t share with its peer threads.
memory.
Data and Code Processes have independent data A thread shares the data segment,
sharing and code segments. code segment, files etc. with its
peer threads.
Treatment by OS All the different processes are All user level peer threads are
treated separately by the treated as a single task by the
operating system. operating system.
Time for creation Processes require more time for Threads require less time for
creation. creation.
Time for Processes require more time for Threads require less time for
termination termination. termination.
Inter-thread communication:
Inter-thread communication in Java is a technique through which multiple threads
communicate with each other.
It provides an efficient way through which more than one thread communicate with each
other by reducing CPU idle time. CPU idle time is a process in which CPU cycles are not
wasted.
When more than one threads are executing simultaneously, sometimes they need to
communicate with each other by exchanging information with each other. A thread
exchanges information before or after it changes its state.
For example, suppose that there are two threads A and B. Thread B uses data produced by
Thread A and performs its task.
If Thread B waits for Thread A to produce data, it will waste many CPU cycles. But if
threads A and B communicate with each other when they have completed their tasks, they do
not have to wait and check each other’s status every time.
Inter-thread communication is a process in which a thread is paused running in its critical
region and another thread is allowed to enter (or lock) in the same critical region to be
executed. i.e. synchronized threads communicate with each other.
Below object class methods are used for Inter-thread communication process:
1. wait(): this method instructs the current thread to release the monitor held by it and to get
suspended until some other threads sends a notification from the same monitor.
2. notify(): this method is used to send the notification to the thread that is suspended by the
wait() method.
3. notifyAll(): this method is used to send the notification to all the threads that are suspended
by wait() method.
Syntax: public void notifyAll().
//starting threads.
p.start();
c.start();
}
}