// this is a similar program from the book just java2 chap 10 pubic class Coffees { public static void main(String args [] ) { Expresso t1 = new Expresso( ); t1.start( ); new Latte( ).start( ); // an anonymous thread } } Class Expresso extends Thread { public void run( ) { while ( true ) { System.out.println(" I like Expresso" ); yield( ); } } } class Latte extends Thread { public void run() { while (true) { System.out.println(" I like Latte!" ); yield( ); } } } class Frappacino implements Runnable { public void run() {
while(true) { System.out.println(" I love Frappacinos! " ); yield(); } } } // This is an example of Multiple Inheritance which does not work // and is a cludge in C++ because of method name clashing. //class Frappacino extends Latte {
//public void run() { //while(true) {
//System.out.println(" I love Frappacinos!" );
//yield( ); //}
// } //} // //
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Can one say that multi-threading is the most difficult on the java programmer and developer tests?
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
I read recently that yield() is unreliable, because it's implemented differently in different OSs. The problem, if I recall correctly, is that, on certain systems (e.g., Solaris?), threads are implemented in such a way that the use of yield() can fail to cause control to switch to a different thread. I think the idea is that, on such systems, another thread will run only if it's of higher priority than the current thread. Are you running on Windows? I believe it's supposed to work on Windows, because of time-slicing. I read about these things recently, on-line in parts of an O'Reilly book on threads.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Yes I use win98 2nd ed. What method or methods should I synchronize so they the thread do not corrupt memory? So it will work......