INTER THREAD COMMINICATION:
Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: wait() notify()
wait()
It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify().
notify()
It wakes up one single thread that called wait() on the same object. It should be noted that calling notify() does not actually give up a lock on a resource.
notifyAll()
It wakes up all the threads that called wait() on the same object.
PROGRAM:
// Java program to demonstrate inter-thread communication
// (wait(), join() and notify()) in Java
import java.util.Scanner;
public class threadexample
{
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();
// Create a thread object that calls pc.produce()
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Create another thread object that calls
// pc.consume()
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
// PC (Produce Consumer) class with produce() and
// consume() methods.
public static class PC
{
// Prints a string and waits for consume()
public void produce()throws InterruptedException
{
// synchronized block ensures only one thread
// running at a time.
synchronized(this)
{
System.out.println("producer thread running");
// releases the lock on shared resource
wait();
// and waits till some other method invokes notify().
System.out.println("Resumed");
}
}
// Sleeps for some time and waits for a key press. After key
// is pressed, it notifies produce().
public void consume()throws InterruptedException
{
// this makes the produce thread to run first.
Thread.sleep(1000);
Scanner s = new Scanner(System.in);
// synchronized block ensures only one thread
// running at a time.
synchronized(this)
{
System.out.println("Waiting for return key.");
s.nextLine();
System.out.println("Return key pressed");
// notifies the produce thread that it
// can wake up.
notify();
// Sleep
Thread.sleep(2000);
}
}
}
}
OUTPUT:
producer thread running
Waiting for return key.
Return key pressed
Resumed
DAEMON THEAD
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection. You can use the setDaemon(boolean) method to change the Thread daemon properties before the threadstarts
PROGRAM:
// Java program to demonstrate the usage of
// setDaemon() and isDaemon() method.
public class DaemonThread extends Thread
{
DaemonThread(String name){
super(name);
}
public void run()
{
// Checking whether the thread is Daemon or not
if(Thread.currentThread().isDaemon())
{
System.out.println(getName() + " is Daemon thread");
}
else
{
System.out.println(getName() + " is User thread");
}
}
public static void main(String[] args)
{
DaemonThread t1 = new DaemonThread("t1");
DaemonThread t2 = new DaemonThread("t2");
DaemonThread t3 = new DaemonThread("t3");
// Setting user thread t1 to Daemon
t1.setDaemon(true);
// starting first 2 threads
t1.start();
t2.start();
// Setting user thread t3 to Daemon
t3.setDaemon(true);
t3.start();
}
}
OUTPUT:
t1 is Daemon thread
t2 is User thread
STACK TRACE ELEMENTS:
An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated.
declaringClass – the fully qualified name of the class containing the execution point represented by the stack trace element.
methodName – the name of the method containing the execution point represented by the stack trace element.
fileName – the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
lineNumber – the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method.
PROGRAM:
// Java code illustrating equals() method
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
StackTraceElement st1 = new StackTraceElement("foo", "fuction1",
"StackTrace.java", 1);
StackTraceElement st2 = new StackTraceElement("bar", "function2",
"StackTrace.java", 1);
Object ob = st1.getFileName();
// checking whether file names are same or not
System.out.println(st2.getFileName().equals(ob));
}
}
OUTPUT:
True
PROGRAM:
// Java code illustrating getClassName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("Class name of each thread involved:");
for(int i = 0; i<2; i++)
{
System.out.println(Thread.currentThread().getStackTrace()[I].
getClassName());
}
}
}
OUTPUT:
Class name of each thread involved: java.lang.Thread StackTraceElementDemo
PROGRAM:
// Java code illustrating getFileName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("file name: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
getFileName());
}
}
OUTPUT:
file name: Thread.java StackTraceElementDemo.java
Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: wait() notify()
wait()
It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify().
notify()
It wakes up one single thread that called wait() on the same object. It should be noted that calling notify() does not actually give up a lock on a resource.
notifyAll()
It wakes up all the threads that called wait() on the same object.
PROGRAM:
// Java program to demonstrate inter-thread communication
// (wait(), join() and notify()) in Java
import java.util.Scanner;
public class threadexample
{
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();
// Create a thread object that calls pc.produce()
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Create another thread object that calls
// pc.consume()
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
// PC (Produce Consumer) class with produce() and
// consume() methods.
public static class PC
{
// Prints a string and waits for consume()
public void produce()throws InterruptedException
{
// synchronized block ensures only one thread
// running at a time.
synchronized(this)
{
System.out.println("producer thread running");
// releases the lock on shared resource
wait();
// and waits till some other method invokes notify().
System.out.println("Resumed");
}
}
// Sleeps for some time and waits for a key press. After key
// is pressed, it notifies produce().
public void consume()throws InterruptedException
{
// this makes the produce thread to run first.
Thread.sleep(1000);
Scanner s = new Scanner(System.in);
// synchronized block ensures only one thread
// running at a time.
synchronized(this)
{
System.out.println("Waiting for return key.");
s.nextLine();
System.out.println("Return key pressed");
// notifies the produce thread that it
// can wake up.
notify();
// Sleep
Thread.sleep(2000);
}
}
}
}
OUTPUT:
producer thread running
Waiting for return key.
Return key pressed
Resumed
DAEMON THEAD
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection. You can use the setDaemon(boolean) method to change the Thread daemon properties before the threadstarts
PROGRAM:
// Java program to demonstrate the usage of
// setDaemon() and isDaemon() method.
public class DaemonThread extends Thread
{
DaemonThread(String name){
super(name);
}
public void run()
{
// Checking whether the thread is Daemon or not
if(Thread.currentThread().isDaemon())
{
System.out.println(getName() + " is Daemon thread");
}
else
{
System.out.println(getName() + " is User thread");
}
}
public static void main(String[] args)
{
DaemonThread t1 = new DaemonThread("t1");
DaemonThread t2 = new DaemonThread("t2");
DaemonThread t3 = new DaemonThread("t3");
// Setting user thread t1 to Daemon
t1.setDaemon(true);
// starting first 2 threads
t1.start();
t2.start();
// Setting user thread t3 to Daemon
t3.setDaemon(true);
t3.start();
}
}
OUTPUT:
t1 is Daemon thread
t2 is User thread
STACK TRACE ELEMENTS:
An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated.
declaringClass – the fully qualified name of the class containing the execution point represented by the stack trace element.
methodName – the name of the method containing the execution point represented by the stack trace element.
fileName – the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
lineNumber – the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method.
PROGRAM:
// Java code illustrating equals() method
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
StackTraceElement st1 = new StackTraceElement("foo", "fuction1",
"StackTrace.java", 1);
StackTraceElement st2 = new StackTraceElement("bar", "function2",
"StackTrace.java", 1);
Object ob = st1.getFileName();
// checking whether file names are same or not
System.out.println(st2.getFileName().equals(ob));
}
}
OUTPUT:
True
PROGRAM:
// Java code illustrating getClassName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("Class name of each thread involved:");
for(int i = 0; i<2; i++)
{
System.out.println(Thread.currentThread().getStackTrace()[I].
getClassName());
}
}
}
OUTPUT:
Class name of each thread involved: java.lang.Thread StackTraceElementDemo
PROGRAM:
// Java code illustrating getFileName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("file name: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
getFileName());
}
}
OUTPUT:
file name: Thread.java StackTraceElementDemo.java