117.线索和同步 - 举例
|
|
public class AddApplet extends Applet implements Runnable {
- Thread t;
- boolean go_ahead = false;
- int a, b, totle;
- public void start() {
- t = new Thread(this);
- t.start();
- }
- public synchronized void run() {
- while ( true ) {
- while ( !go_ahead ) wait(); // wait for notify signal
- totle = a + b;
- go_ahead = false;
- System.out.println("totle = "+totle);
- }
- }
- public synchronized void adder(int i, int j) {
- a = i; b = j;
- notify(); // Notify the thread that a,b are ready
- }
}
Copyright: NPACT |
|