Welcome To Uday Satya Blog

61-70 PROGRAMS



61  Program to Demonstrate Thread Priorities


class A extends Thread

{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println("\tfrom Thread A : i = " +i);
System.out.println("Exit from A");
}}

class B extends Thread{

public void run(){
for(int j=1;j<=5;j++)
System.out.println("\tfrom Thread B : j = " +j);
System.out.println("Exit from B");
}}

class C extends Thread{
public void run(){
for(int k=1;k<=5;k++)
System.out.println("\tfrom Thread C : k = " +k);
System.out.println("Exit from C");
}}

 class Prioritytest {

public static void main(String args[]){
A a = new A();
B b = new B();
C c = new C();
c.setPriority(Thread.MAX_PRIORITY);
b.setPriority(a.getPriority()+1);
a.setPriority(Thread.MIN_PRIORITY);
a.start();
b.start();
c.start();
}
}

OUTPUT :

        from Thread C : k = 1
        from Thread B : j = 1
        from Thread A : i = 1
        from Thread C : k = 2
        from Thread B : j = 2
        from Thread A : i = 2
        from Thread C : k = 3
        from Thread B : j = 3
        from Thread A : i = 3
        from Thread C : k = 4
        from Thread B : j = 4
        from Thread A : i = 4
        from Thread C : k = 5
        from Thread B : j = 5
        from Thread A : i = 5
Exit from C
Exit from B
Exit from A       



62  Program to Demonstrate Synchronized Block


class Callme

{
void call(String msg)
{
System.out.print("[" +msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}

class Caller implements Runnable

{ 
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run()
{
synchronized(target) {
target.call(msg);
}}}

class SynchBlock
{
public static void main(String args[])
{
Callme target=new Callme();
Caller ob1=new Caller(target, "Hello");
Caller ob2=new Caller(target, "Synchronized");
Caller ob3=new Caller(target, "World");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}


OUTPUT :

[Hello]
[Synchronized]
[World]


63   Program to Demonstrate Synchronized Method


class Callme

{
synchronized void call(String msg)
{
System.out.print("[" +msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}}

class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run()
{
target.call(msg);
}}

class SynchMeth
{
public static void main(String args[])
{
Callme target=new Callme();
Caller ob1=new Caller(target, "Hello");
Caller ob2=new Caller(target, "Synchronized");
Caller ob3=new Caller(target, "World");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}


OUTPUT :

[Hello]
[Synchronized]
[World]


64   Program to Demonstrate Deadlock

class A {
synchronized void foo(B b) {
String name=Thread.currentThread().getName();
System.out.println(name+ " entered A.foo");
try {
Thread.sleep(1000);
}
catch(Exception e) {
System.out.println("A Interrupted");
}
System.out.println(name+ " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("Inside A.last");
}}

class B {

synchronized void bar(A a) {
String name=Thread.currentThread().getName();
System.out.println(name+ " entered B.bar");
try {
Thread.sleep(1000);
}
catch(Exception e) {
System.out.println("B Interrupted");
}
System.out.println(name+ " trying to call A.last()");
a.last();
}
synchronized void last() {
System.out.println("Inside A.last");
}}

class Deadlock implements Runnable
{
A a=new A();
B b=new B();
Deadlock()
{
Thread.currentThread().setName("mainThread");
Thread t=new Thread(this, "RacingThread");
t.start();
a.foo(b);
System.out.println("Back in main thread");
}
public void run()
{
b.bar(a);
System.out.println("Back in other thread");
}
public static void main(String args[])
{
new Deadlock();
}
}


OUTPUT :

mainThread entered A.foo
RacingThread entered B.bar
mainThread trying to call B.last()
RacingThread trying to call A.last()


65  Program to Demonstrate Thread Methods

class Q {
int n;
boolean valueSet= false;
synchronized int get() {
if(!valueSet)
try {
wait();
}
catch(InterruptedException ie) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n) {
if(valueSet)
try {
wait();
}
catch(InterruptedException ie) {
System.out.println("InterruptedException caught");
}
this.n=n;
valueSet=true;
System.out.println("Put: " + n);
notify();
}}

class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q=q;
new Thread(this, "Producer").start();
}
public void run() {
int i=0;
while(true) {
q.put(i++);
}}}

class Consumer implements Runnable {

 Qq;
Consumer(Q q) {
this.q=q;
new Thread(this, "Consumer").start();
}
public void run() {
int i=0;
while(true) {
q.get();
}}}

class PCFixed {

public static void main(String args[]) {
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}}

OUTPUT :

Put : 1
Got : 1
Put : 2
Got : 2
Put : 3
Got : 3
Put : 4
Got : 4
Put : 5
Got : 5



66  Program to Create Threads using Thread Class


class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println("\tfrom Thread A : i = " +i);
System.out.println("Exit from A");
}
}

class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
System.out.println("\tfrom Thread B : j = " +j);
System.out.println("Exit from B");
}
}

class Threadtest
{
public static void main(String args[])
{
new A().start();
new B().start();
try{
Thread.sleep(10);
}
catch(InterruptedException e)
{
System.out.println("Exception " +e);
}
}
}


OUTPUT :

        from Thread A : i = 1
        from Thread B : j = 1
        from Thread A : i = 2
        from Thread B : j = 2
        from Thread A : i = 3
        from Thread B : j = 3
        from Thread A : i = 4
        from Thread B : j = 4
        from Thread A : i = 5
        from Thread B : j = 5
Exit from A
Exit from B



67  Program to Demonstrate Join() & isAlive() methods

class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname) {
name=threadname;
t= new Thread(this,name);
System.out.println("New Thread: "+t);
t.start();
}
public void run() {
try {
for(int i=5;i>0;i--) {
System.out.println(name + ": " +i);
Thread.sleep(1000);
}}
catch (InterruptedException ie) {
System.out.println(name + "interrupted."); }
System.out.println(name + "exiting.");
}}

class DemoJoin {

public static void main(String args[]) {
NewThread ob1= new NewThread("One");
NewThread ob2= new NewThread("Two");
NewThread ob3= new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch (InterruptedException ie) {
System.out.println("Main thread Interrupted"); }
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}}

OUTPUT :

New Thread: Thread[One,5,main]
New Thread: Thread[Two,5,main]
One: 5
Two: 5
New Thread: Thread[Three,5,main]
Thread One is alive: true
Three: 5
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
Two: 4
One: 4
Three: 4
Two: 3
One: 3
Three: 3
Two: 2
One: 2
Three: 2
Two: 1
One: 1
Three: 1
Twoexiting.
Oneexiting.
Threeexiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.



68  Program to Demonstrate Get User Input from Keyboard

import java.io.*;
 class GetUserInput
{
 public static void main(String[] args)
{
 //the data that will be entered by the user
String name;
 //an instance of the BufferedReader class
//will be used to read the data
BufferedReader reader;
//specify the reader variable
//to be a standard input buffer
reader = new BufferedReader(new InputStreamReader(System.in));
//ask the user for their name
System.out.print("What is your name? ");
try{
//read the data entered by the user using
//the readLine() method of the BufferedReader class
 //and store the value in the name variable
 name = reader.readLine();
//print the data entered by the user
System.out.println("Your name is " + name);
}
catch (IOException ioe){
//statement to execute if an input/output exception occurs
 System.out.println("An unexpected error occured.");
 }
}
}

OUTPUT :
What is your name? Uday Satya
Your name is Uday Satya


69 Program to Create a Frame Window using Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class FrameTest extends Frame {
FrameTest(String title) {
super(title);
MyWindowAdapter adap=new MyWindowAdapter(this);
addWindowListener(adap);
}
public void paint(Graphics g) {
g.drawString("This is in frame window",10,40);
}}
class MyWindowAdapter extends WindowAdapter {

FrameTest ft;
public MyWindowAdapter(FrameTest ft) {
this.ft=ft;
}
public void WindowClosing(WindowEvent we) {
ft.setVisible(false);
}}
public class AppletFrameWindowEx1 extends Applet {
Frame f;
public void init() {
f=new FrameTest("A frame Window");
f.setSize(300,300);
f.setVisible(true);
}
public void start() {
f.setVisible(true);
}
public void stop() {

f.setVisible(false);
}
public void paint(Graphics g) {
g.drawString("This is a Test applet",10,20);
}}


70  Program to Demonstrate Life cycle of methods in Applet

import java.awt.*;
import java.applet.*;
public class Sample extends Applet {
String msg;
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
msg="Inside init()--";
}
public void start() {
msg +="Inside start()--";
}
public void paint(Graphics g) {
msg +="Inside paint()--";
g.drawString(msg,10,30);
}}

0 comments:

Post a Comment