Skip to content

Instantly share code, notes, and snippets.

@jeeeyul
Created July 23, 2015 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeeeyul/686135b087b3f91ad104 to your computer and use it in GitHub Desktop.
Save jeeeyul/686135b087b3f91ad104 to your computer and use it in GitHub Desktop.
ObjectMonitorExample.java
package locktest;
import java.text.MessageFormat;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ObjectMonitorExample implements Runnable {
static Object lock = new Object();
static int count = 5;
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button btn = new Button(shell, SWT.PUSH);
btn.setText(MessageFormat.format("unlock:{0}", count));
btn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
synchronized (lock) {
count--;
lock.notify();
}
btn.setText(MessageFormat.format("unlock:{0}", count));
}
});
new Thread(new ObjectMonitorExample()).start();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
@Override
public void run() {
System.out.println("대기중입니다.");
try {
synchronized (lock) {
while (count > 0) {
System.out.println(MessageFormat.format("{0}개의 작업을 기다리는 중.", count));
lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("대기 종료");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment