



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























106
A Program with Three Java Threads Write a program that creates 3 threads 107
class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\t From ThreadC: k= "+k); } System.out.println("Exit from C"); } } class ThreadTest { public static void main(String args[]) { new A().start(); new B().start(); new C().start(); } } 109
Exit from B
ThreadName.setPriority(intNumber)
112
class A extends Thread { public void run() { System.out.println("Thread A started"); for(int i=1;i<=4;i++) { System.out.println("\t From ThreadA: i= "+i); } System.out.println("Exit from A"); } } class B extends Thread{ public void run() { System.out.println("Thread B started"); for(int j=1;j<=4;j++) { System.out.println("\t From ThreadB: j= "+j); } System.out.println("Exit from B"); } } 113
threadC.setPriority(Thread.MAX_PRIORITY); threadB.setPriority(threadA.getPriority()+1); threadA.setPriority(Thread.MIN_PRIORITY); System.out.println("Started Thread A"); threadA.start(); System.out.println("Started Thread B"); threadB.start(); System.out.println("Started Thread C"); threadC.start(); System.out.println("End of main thread"); } }
115 docsity.com
Deposit() Withdraw() Enquire() 116
…
118
the driver: 3 rd Threads sharing the same object 119 class InternetBankingSystem { public static void main(String [] args ) { Account accountObject = new Account (); Thread t1 = new Thread(new MyThread( accountObject )); Thread t2 = new Thread(new YourThread( accountObject )); Thread t3 = new Thread(new HerThread( accountObject )); t1.start(); t2.start(); t3.start(); // DO some other operation } // end main() }
Monitor (shared object access): serializes operation on shared object class Account { // the 'monitor' int balance;
} 121
122 Server Threads Message Passing Facility Server Process Client Process Client Process
Introduction:
124 docsity.com
The String class supports several constructors. To create an empty String, you call the default constructor. For example, String s=new String(); will create an instance of String with no characters in it. The String class provides a variety of constructors to handle this. To create a String initialized by an array of characters, use the constructor shown here: String(char chars[ ]) Here is an example: char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); This constructor initializes s with the string “abc”. 6/24/
125 docsity.com