今天看了一个关于sleep的面试题,然后自己想了一个关于wait的 具体实现是这样的
有三个buton 分别是hello stop wake 实现的功能是 当点击hello就在控制台输出一个hello 然后点击stop 输出hello就wait指导再点击wake然后中间点了几次hello就一下输出几个hello下面是我写的 但是写不对,线程这东西好久不用都忘了,希望各位指点一下。import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
private JButton sayHello;
private JButton stop;
private JButton wake;
private boolean flag; public static void main(String[] args) {
new WaitNotifyTest().launch();
} public void launch() {
sayHello = new JButton("hello");
stop = new JButton("stop");
wake = new JButton("wake");
flag = true;
add(sayHello);
add(stop);
add(wake);
sayHello.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
while (flag == false) {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("hello");
}
}); stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
}
}); wake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (this) {
flag = true;
this.notify();
}
}
});
this.setLayout(new FlowLayout());
this.pack();
this.setVisible(true);
}}
有三个buton 分别是hello stop wake 实现的功能是 当点击hello就在控制台输出一个hello 然后点击stop 输出hello就wait指导再点击wake然后中间点了几次hello就一下输出几个hello下面是我写的 但是写不对,线程这东西好久不用都忘了,希望各位指点一下。import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
private JButton sayHello;
private JButton stop;
private JButton wake;
private boolean flag; public static void main(String[] args) {
new WaitNotifyTest().launch();
} public void launch() {
sayHello = new JButton("hello");
stop = new JButton("stop");
wake = new JButton("wake");
flag = true;
add(sayHello);
add(stop);
add(wake);
sayHello.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
while (flag == false) {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("hello");
}
}); stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
}
}); wake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (this) {
flag = true;
this.notify();
}
}
});
this.setLayout(new FlowLayout());
this.pack();
this.setVisible(true);
}}
解决方案 »
- httpclient 的问题啊~~~~~~~~~~~~~~~~~希望大家能帮助一下啊~~~~~~~~~~~~55555555555555555555555555
- 请问这程序为什么说我找不到pagesInGroup符号?
- socket通信问题
- 请教如何将一张256色的GIF转换成16色的?
- 学软件开发和英语的联系→本人很迷茫,希望高手说下。。
- Thinking in Java的一个练习题,请帮忙!
- 有一个GUI的问题~~~~~解决就给分~~~~~
- Applet滚动条的问题
- 请问怎样判断一个字符是中文字符还是英文字符?(空)
- 今天毕设检查,让我很郁闷!!
- Java中io操作,必须先创建文件夹再创建文件吗
- 文件复制异常java.nio.channels.ClosedChannelException
修改了一下,楼主参考:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
private JButton sayHello;
private JButton stop;
private JButton wake;
private boolean flag; static Object ob=new Object(); //用于做同步对象锁。
public static void main(String[] args) {
new WaitNotifyTest().launch();
} public void launch() {
sayHello = new JButton("hello");
stop = new JButton("stop");
wake = new JButton("wake");
flag = true;
add(sayHello);
add(stop);
add(wake);
sayHello.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new Thread(new Runnable(){ //内部类再创建并启动一个线程
public void run(){
while (flag == false) {
synchronized (WaitNotifyTest.ob) {
try { WaitNotifyTest.ob.wait(); //不用this了. } catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("hello");
}
}).start();
}
}); stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
}
}); wake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (WaitNotifyTest.ob) { //这的同步对象也变了。
flag = true;
WaitNotifyTest.ob.notify();
}
}
});
this.setLayout(new FlowLayout());
this.pack();
this.setVisible(true);
}}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
private JButton sayHello;
private JButton stop;
private JButton wake;
private boolean flag; public static void main(String[] args) {
WaitNotifyTest t= new WaitNotifyTest();//改哈这里 this不要乱用 由于直接new出来的东西没有引用,不是实体不能被引用
t.launch(); //楼主接分吧
} public void launch() {
sayHello = new JButton("hello");
stop = new JButton("stop");
wake = new JButton("wake");
flag = true;
add(sayHello);
add(stop);
add(wake);
sayHello.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
while (flag == false) {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("hello");
}
}); stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
}
}); wake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (this) {
flag = true;
this.notify();
}
}
});
this.setLayout(new FlowLayout());
this.pack();
this.setVisible(true);
}}