Professional Documents
Culture Documents
//MyThread.java
class MyThread implements Runnable
{
String message;
MyThread(String msg)
{
message = msg;
}
public void run()
{
for(int count=0;count<=5;count++)
{
try
{
System.out.println("Run method: " + message);
Thread.sleep(100);
}
catch (InterruptedException ie)
{
System.out.println("Exception in thread:
"+ie.getMessage());
}
}
}
}
//Mainthread.java
public class MainThread
{
public static void main(String[] args)
{
MyThread obj1 = new MyThread("MyThread obj1");
MyThread obj2 = new MyThread("MyThread obj2");
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.start();
t2.start();
}
}
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
//SynchTest.java
class SynchTest
{
void printNumber(int n)
{
synchronized(this)
{
System.out.println("Table of "+n);
System.out.println("==========");
for(int i=1;i<=10;i++)
{
System.out.println(n+" * "+i+" = "+(n*i));
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
//MyThread1.java
class MyThread1 extends Thread
{
SynchTest t;
MyThread1(SynchTest t)
{
this.t=t;
}
public void run()
{
t.printNumber(7);
}
}
//SynchronizedBlock.java
public class SynchronizedBlock
{
public static void main(String args[])
{
SynchTest obj = new SynchTest();
MyThread1 t1=new MyThread1(obj);
t1.start();
}
}
//Thread1.java
class Thread1 extends Thread
{
String msg = "";
Thread1(String msg)
{
this.msg = msg;
}
public void run()
{
try
{
while (true)
{
System.out.println(msg);
Thread.sleep(300);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//Thread2.java
class Thread2 extends Thread
{
String msg = "";
Thread2(String msg)
{
this.msg = msg;
}
public void run()
{
try
{
while (true)
{
System.out.println(msg);
Thread.sleep(400);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//ThreadDemo.java
class ThreadDemo
{
public static void main(String[] args)
{
Thread1 t1 = new Thread1("Running Thread1....");
Thread1 t2 = new Thread1("Running Thread2....");
t1.start();
t2.start();
}
}
//Fibonacci.java
import java.io.*;
class Fibonacci extends Thread
{
public void run()
{
try
{
int a=0, b=1, c=0;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println("\n=================================");
System.out.println("Fibonacci series:");
while (n>0)
{
System.out.print(c+" ");
a=b;
b=c;
c=a+b;
n=n-1;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//Reverse.java
class Reverse extends Thread
{
public void run()
{
try
{
System.out.println("\n=================================");
System.out.println("\nReverse is: ");
System.out.println("=================================");
for (int i=10; i >= 1 ;i-- )
{
System.out.print(i+" ");
}
System.out.println("\n=================================\n\n");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//MainThread.java
class MainThread
{
public static void main(String[] args)
{
try
{
Fibonacci fib = new Fibonacci();
fib.start();
fib.sleep(4000);
Reverse rev = new Reverse();
rev.start();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
(9). Write a program to demonstrate the Synchronization block. In program you have
to print tables of any
number but synchronized the block in manner so that no other table thread can
executes when already one thread
is running.
import java.lang.*;
class Table{
public synchronized void table(int n){
for(int i=1;i<=10;i++){
System.out.println(""+n+"*"+i+"="+n*i);
}
}
}
class SyncDemo{
public static void main(String args[]) throws Exception{
Table t=new Table();
Thread t1=new Thread(new Runnable()
{
public void run(){
for(int i=2;i<4;i++){
t.table(i);
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}