Program 1:
class Demo1 {
public static void main(String args[])
{
Thread t1 = Thread.currentThread();
System.out.println("Its name ="+t1.getName());
}
}
Program 2:
Write a Program to creat MyThread class with run() method and then attach a thread to this MyThread
class object.
class MyThread extends Thread {
public void run()
{
for(int i=1;i<=100;i++)
{
System.out.println(i);
}
}
}
public class Demo1 {
public static void main(String[] args) {
MyThread obj = new MyThread();
Thread t=new Thread(obj);
t.start();
Program 3 : Re write the program showing how to terminate the thread by pressing the enter button.
import java.io.*;
class MyThread extends Thread {
boolean stop=false;
public void run()
{
for(int i=1;i<=100;i++)
{
System.out.println(i);
if(stop) return;
}
}
}
public class Demo1 {
public static void main(String[] args) throws IOException {
MyThread obj = new MyThread();
Thread t=new Thread(obj);
t.start();
System.in.read();
obj.stop=true;
}}
Program 4: Write a program showing execution of multiple tasks with a a single
thread.
class MyThread implements Runnable {
public void run()
{
task1();
task2();
task3();
}
void task1()
{
System.out.println("this is task 1");
}
void task2()
{
System.out.println("this is task 2");
}
void task3()
{
System.out.println("this is task 3");
}
}
class Demo1 {
public static void main(String args[])
{
MyThread obj1=new MyThread();
Thread t1=new Thread(obj1);
t1.start();
}
}
Program 5: Write a program showing two thread working simultaneously upon two
objects.
class MyThread implements Runnable {
String str;
MyThread(String str){
this.str=str;
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(str+ ":"+i);
try{
Thread.sleep(2000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
class Demo1 {
public static void main(String args[])
{
MyThread obj1=new MyThread("cutthe ticket");
MyThread obj2=new MyThread("show the seat");
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}
}
Program 6 : Write a program showing two threads acting upon a single object.
class Reserve implements Runnable {
int available = 1;
int wanted;
Reserve(int i)
{
wanted=i;
}
public void run()
{
System.out.println("Available berths="+available);
if(available>=wanted)
{
String name = Thread.currentThread().getName();
System.out.println(wanted +" Berths reserved for " +name);
try {
Thread.sleep(1500);
available=available-wanted;
}catch(InterruptedException ie){}
}
else System.out.println("Sorry,no berths");
}
}
class Demo1 {
public static void main(String args[])
{
Reserve obj=new Reserve(1);
Thread t1=new Thread(obj);
Thread t2= new Thread(obj);
t1.setName("First Person");
t2.setName("Second Person");
t1.start();
t2.start();
}
}
Program 7 Write a Program to synchronize the threads acting on the same
object. The synchronized block in the program can be executed by only one
thread at a time
class Reserve implements Runnable {
int available = 1;
int wanted;
Reserve(int i)
{
wanted=i;
}
public void run()
{
synchronized(this){
System.out.println("Available berths="+available);
if(available>=wanted)
{
String name = Thread.currentThread().getName();
System.out.println(wanted +" Berths reserved for " +name);
try {
Thread.sleep(1500);
available=available-wanted;
}catch(InterruptedException ie){}
}
else System.out.println("Sorry,no berths");
}
}
}
class Demo1 {
public static void main(String args[])
{
Reserve obj=new Reserve(1);
Thread t1=new Thread(obj);
Thread t2= new Thread(obj);
t1.setName("First Person");
t2.setName("Second Person");
t1.start();
t2.start();
}
}
Program 8 :Write a program which show situation depiction deadlocks
class BookTicket extends Thread
{
Object train, comp;
BookTicket(Object train,Object comp)
{
this.train=train;
this.comp=comp;
}
public void run()
{
synchronized(train)
{
System.out.println("BookTicket locked on train");
try{
Thread.sleep(150);
}catch(InterruptedException ie){}
}
System.out.println("BookTicket now waiting to lock on
compartment..");
synchronized(comp)
{
System.out.println("BookTicket locked on
compartment");
}
}
}
class CancelTicket extends Thread {
Object train, comp;
CancelTicket(Object train,Object comp)
{
this.train=train;
this.comp=comp;
}
public void run()
{
synchronized(comp)
{
System.out.println("CancelTicket locked on compartment");
try{
Thread.sleep(150);
}catch(InterruptedException ie){}
System.out.println("CancelTicket now waiting to lock on
train");
synchronized(train)
{
System.out.println("CancelTicket locked on train");
}
}
class Demo1{
public static void main(String args[]){
Object train=new Object();
Object compartment=new Object();
BookTicket obj1=new BookTicket(train,compartment);
CancelTicket obj2=new CancelTicket(train,compartment);
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}}
Program 9 : Write a program where the consumer thread checks whether the data
production is over or now every 10 milliseconds.
class Demo1 {
public static void main(String[] args) throws Exception
{
Producer obj1=new Producer();
Consumer obj2 = new Consumer(obj1);
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}
}
class Producer extends Thread
{
StringBuffer sb;
boolean dataprodover=false;
Producer()
{
sb=new StringBuffer();
}
public void run()
{
for(int i=1;i<=10;i++) {
try {
sb.append(i+":");
Thread.sleep(100);
System.out.println("appending");
}catch(Exception e){}
}
dataprodover=true;
}
}
class Consumer extends Thread {
Producer prod ;
Consumer(Producer prod)
{
this.prod=prod;
}
public void run()
{
try{
while(!prod.dataprodover)
Thread.sleep(10);
}catch(Exception e){}
System.out.println(prod.sb);
}
}
Program 10 : Write a program such that the consumer thread is informed
immediately when the data production is over.
class Demo1 {
public static void main(String[] args) throws Exception
{
Producer obj1=new Producer();
Consumer obj2 = new Consumer(obj1);
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}
}
class Producer extends Thread
{
StringBuffer sb;
Producer()
{
sb=new StringBuffer();
}
public void run()
{
synchronized(sb){
for(int i=1;i<=10;i++) {
try {
sb.append(i+":");
Thread.sleep(100);
System.out.println("appending");
}catch(Exception e){}
}
sb.notify();
}
}
}
class Consumer extends Thread {
Producer prod ;
Consumer(Producer prod)
{
this.prod=prod;
}
public void run()
{
synchronized(prod.sb){
try{
prod.sb.wait();
}catch(Exception e){}
System.out.println(prod.sb);
}
}
}
Program 11: Write a program to understand the thread priorities.
class MyClass extends Thread {
int count =0;
public void run()
{
for(int i=1;i<10000;i++)
count++;
System.out.println("completed thread
"+Thread.currentThread().getName());
System.out.println("Its priority is"+
Thread.currentThread().getPriority());
}
}
class Demo1 {
public static void main(String args[]){
MyClass obj = new MyClass();
Thread t1 = new Thread(obj,"one");
Thread t2 = new Thread(obj,"one");
t1.setPriority(2);
t2.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
}
}