aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why doesn't this  Multithreading program work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't this Multithreading program work?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

// 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
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
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
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......

Please check out my other posts here:
http://www.javaranch.com/ubb/Forum24/HTML/003873.html
http://www.javaranch.com/ubb/Forum24/HTML/003889.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't this Multithreading program work?