File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this Multithreading program work?

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator

// 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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Can one say that multi-threading is the most difficult on
the java programmer and developer tests?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic