三个类放在同一个目录中,甚至放在同一个文件中,一个类声明为public即可. 不需互相import. class A { Object isWindowReady = new Object(); Frame frame = null; public static void main(String[ ] args) { A o = new A(); if(true) { new CreateWindow2(o).start(); } else { // I would prefer this approach new CreateWindow(o).start(); new CloseWindow(o).start(); } } } class CreateWindow extends Thread { A o = null; public CreateWindow(A o) { this.o = o; } public void run() { synchronized(o.isWindowReady) { o.frame = new JFrame(); o.isWindowReady.notify(); } } } class CreateWindow2 extends Thread { A o = null; public CreateWindow2(A o) { this.o = o; } public void run() { new CloseWindow(o).start(); synchronized(o.isWindowReady) { o.frame = new JFrame(); o.isWindowReady.notify(); } } } class CloseWindow extends Thread { A o = null; public CloseWindow(A o) { this.o = o; } public void run() { synchronized(o.isWindowReady) { if(o.frame == null ) { try{ o.isWindowReady.wait(); }catch(InterruptedException e) {} } o.frame = null; } } }
就可以用第一个类了.
不需互相import.
class A {
Object isWindowReady = new Object();
Frame frame = null;
public static void main(String[ ] args) {
A o = new A();
if(true) {
new CreateWindow2(o).start();
} else { // I would prefer this approach
new CreateWindow(o).start();
new CloseWindow(o).start();
}
}
}
class CreateWindow extends Thread {
A o = null;
public CreateWindow(A o) {
this.o = o;
}
public void run() {
synchronized(o.isWindowReady) {
o.frame = new JFrame();
o.isWindowReady.notify();
}
}
}
class CreateWindow2 extends Thread {
A o = null;
public CreateWindow2(A o) {
this.o = o;
}
public void run() {
new CloseWindow(o).start();
synchronized(o.isWindowReady) {
o.frame = new JFrame();
o.isWindowReady.notify();
}
}
}
class CloseWindow extends Thread {
A o = null;
public CloseWindow(A o) {
this.o = o;
}
public void run() {
synchronized(o.isWindowReady) {
if(o.frame == null ) {
try{
o.isWindowReady.wait();
}catch(InterruptedException e) {}
}
o.frame = null;
}
}
}