0% found this document useful (0 votes)
5 views

Java Notes

Java programmer

Uploaded by

thejasbj999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Notes

Java programmer

Uploaded by

thejasbj999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

class NewThread implements Runnable {

String name; // name of thread


Thread t;
NewThread (String threadname) {
name = threadname;
t = new Thread (this, name) i
System. out.println ("New thread: " + t) ;
t.start (); // Start the thread
)
public void run () {
try {
for (int i = 5; i> 0; i--) {
System. out.println (name + ": " + i);
Thread. sleep (1000) ;

) catch (InterruptedException e) {
System. out.println (name + "Interrupted") ;

System. out.println (name + " exiting. "):

class Multi {
public static void main(String args[]) {
new NewThread("One"); / start threads
new NewThread ("Two") ;
new NewThread ("Three");
try {
Thread.sleep (10000);
) catch (InterruptedException e) {
System. out.println ( "Main thread Interrupted") :
System., out.println("Main thread exiting. ")
)
Producer Consumer Problem:
Let's now work through an example that uses wait( ) and notify( ). To begin, consider the following sample
program that incorrectly implements a simple form of the producer/ consumer problem. It consists of four
classes: Q, the queue that you're trying to synchronize; Producer, the threaded object that is producing
queue entries; Consumer, the threaded object that is consuming queue entries; and PC, the tiny class that
creates the single Q, Producer, and Consumer.

class Q {
int n;
boolean valueSet = false;
synchronized int get() (
while(!valueSet)
try (
wait():
}catch(InterruptedException e) {
System.out.printin("InterruptedException caught"):

If(n>0 && n<10)(


System.out.printIn("Got:" + n);

valueSet= false;
notify():
return n;

synchronized void put(int n) {


while(valueSet)
try {
wait():
}catch(InterruptedException e) (
System.out.printin("InterruptedException caught");
this.n = n;
valueSet = true;
System.out.printIn("Put: " + n);
notify():

class Producer implements Runnable {


Q4:
Producer(Q ){
this.q = q;
new Thread(this, "Producer").start():

public void run() {


int i= 0;
while(true) (
if(i<10)
4-put(i++);

class Consumer implements Runnable {


Q q:
Consumer(Q ) {
this.q =g:
new Thread(this, "Consumer").start():
14 /15
public void run() {
while(true) {
4-get():
)
class PCFixed {
public static void main(String args[O) {
Qq= new Q0:
new Producer(q):
new Consumer(q):
System.out.println("Press Control-C to stop."):
)

Ontout:
Put: 0
Press Control-c to stop.
Got: 0
Put: 1
Got: 1
Put :
Got: 2
Put:3
Got:3
Put: 4
Got: 4
Put: 5
Got: 5
Put: 6
Got: 6
Put: 7
Got: 7
Put: 8
Got: 8
Put: 9
Got: 9
public MyThread (String name) {
super(nane);

eOverride
public void run()

for (int 1 = 1; i « 5; itt) (


Systen.out. printin(Thread. curtentThread(0-getNane() + ": "+ î);
try (

Thread.slecp( 500) ; I/ Sleep for 500 milliseconds


} catch (InterruptedException e)
e.printStackTrace();

public class ThreadPriorityExanple (


public static void main(Stzing[] azgs) (
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2*);

1| Set priorities
thread1. setPriority(Thread. MTN_ PRIORITY); I/ 1
thread2.setPriority(Thread, MAX_PRIORIT); / 10
1| Start the threads
thread1.start();
thread2,start ();
Thread 1: 1

Thread 2: 1
Thread 1: 2
Thread 2: 2
class Oemo enteds Exapton
f

int azio, be0, C;


Cbo)
Deno(bivigjon not pHLle
thmw nw
Cea/b;
S-op(c):
ateh(Excaptem e)

Divi~im nt fotlibla
Finally block exeuted
chained Excphimns
amothes encafion itd an eruftion: the deiand exafhon

’for enamle, inmgine a latonn wkieh a nekod Hloy


atmpt to divide by Jlo.
tithmehcxcafton beeuse an wes that on
a ptlem
Honener,the actad Caue to be det improfuly
the diisor
elor oewed, shich causedcuhiny thlaw ay Aitakebxghon
te method mut ocued,yau migkt alo wat t
that is the lar that
linca
Bnan tat the undalyig
Jet the tallig lode heudle ty and othe
chained excaptins det yau
Ib elsr.
Lithation in skich Jayes
Cas Deno
Atic vod divida()
jt as, be0;

Aithkebaftm al=he

ae.initlaue (nes ToExcphion(Cause ):

S.o.Pals):

You might also like