wait一般在多个线程共享临界区时使用
唤醒是由其他线程来唤醒
你这里只有一个线程,它wait后如何唤醒自己呢?所以可以使用suspend和resume
如:
synchronized(this)
{
if(isWait)
this.suspend();
}t.isWait=false;
//t.n();
//t.notify();
t.resume();功能可以实现,不过java不建议用这2个方法,不好意思,替代的方法还没有试过,可以自行参考看看。
唤醒是由其他线程来唤醒
你这里只有一个线程,它wait后如何唤醒自己呢?所以可以使用suspend和resume
如:
synchronized(this)
{
if(isWait)
this.suspend();
}t.isWait=false;
//t.n();
//t.notify();
t.resume();功能可以实现,不过java不建议用这2个方法,不好意思,替代的方法还没有试过,可以自行参考看看。
解决方案 »
- 这段代码是线程安全的吗?
- 求《良葛格Java JDK 5.0学习笔记》后面几章!
- 我的jdk 是jdk1.4.2 求 tomcat
- 菜鸟问题,高手帮忙
- 使用HIBERNATE时出现异常提示no dialect mapping for JDBC type:3,请问高手如何解决?
- 请问在jBuilder x中怎么样把某一行变成红色
- jbuilder8中,编写代码时,光标实际位置前移,怎么解决?
- 对swing比较熟,而且对这方面工作比较感兴趣的大虾请进
- 从C++转Java的程序员有没有感觉Java的命名规范特别不爽啊?
- 一个简单java程序,链表实现多项式相加
- 求MD5和SHA-1的java实现
- 请问Properties类如何存数据呢?
一个你说的这两个方法回造成对象的损坏。我看了corejava中建议的代替方法和和用notify()差不多。
关键是为什么不能实现那?
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;public class WaitAndNotify
{
public static void main(String[] args)
{
System.out.println("Hello World!");
WaitAndNotifyJFrame frame = new WaitAndNotifyJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class WaitAndNotifyJFrame extends JFrame
{
public WaitAndNotifyJFrame()
{
setSize(300,100);
setLocation(250,250);
JPanel panel = new JPanel();
JButton start = new JButton(new AbstractAction("Start")
{
public void actionPerformed(ActionEvent event)
{
if(t == null)
{
t = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
t1 = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
t.start();
t1.start();
}
else
{
t.isWait=false;
t1.isWait=true;
}
}
});
panel.add(start);
JButton pause = new JButton(new AbstractAction("Pause")
{
public void actionPerformed(ActionEvent e)
{
t.isWait = true;
t1.isWait=false;
}
});
panel.add(pause);
JButton end = new JButton(new AbstractAction("End")
{
public void actionPerformed(ActionEvent e)
{
t.interrupt();
t = null;
}
});
panel.add(end);
getContentPane().add(panel);
}
private WaitAndNotifyThread t,t1;
}
class WaitAndNotifyThread extends Thread
{
public static Object ob=new Object();
public WaitAndNotifyThread(WaitAndNotifyJFrame f)
{
control = f;
isWait = false;
count = 0;
}
public void run()
{
try
{
synchronized(ob){
while(true)
{
System.out.println(Thread.currentThread().getName()+"Count:"+count++);
sleep(1000);
if(isWait){
System.out.println(Thread.currentThread().getName()+"wait");
ob.notify();
ob.wait();
}
}
}
}catch(Exception e){}
}
public void n()
{
notify();
}
public boolean isWait;
private WaitAndNotifyJFrame control;
private int count;
}这是改的一个例子
t和t1互斥运行
wait和notify作用在同一个对象ob上。但不能依靠ob自己唤醒自己。
这是用JFrame作为锁的CODE
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;public class WaitAndNotifyFromWeb
{
public static void main(String[] args)
{
System.out.println("Hello World!");
WaitAndNotifyJFrame frame = new WaitAndNotifyJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class WaitAndNotifyJFrame extends JFrame
{
public WaitAndNotifyJFrame()
{
setSize(300,100);
setLocation(250,250);
JPanel panel = new JPanel();
JButton start = new JButton(new AbstractAction("Start")
{
public void actionPerformed(ActionEvent event)
{
if(t == null)
{
t = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
t1 = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
t.start();
t1.start();
}
else
{
t.isWait=false;
t1.isWait=true;
}
}
});
panel.add(start);
JButton pause = new JButton(new AbstractAction("Pause")
{
public void actionPerformed(ActionEvent e)
{
t.isWait = true;
t1.isWait=false;
}
});
panel.add(pause);
JButton end = new JButton(new AbstractAction("End")
{
public void actionPerformed(ActionEvent e)
{
t.interrupt();
t = null;
}
});
panel.add(end);
getContentPane().add(panel);
}
private WaitAndNotifyThread t,t1;
}
class WaitAndNotifyThread extends Thread
{
public static Object ob=new Object();
public WaitAndNotifyThread(WaitAndNotifyJFrame f)
{
control = f;
isWait = false;
count = 0;
}
public void run()
{
try
{
synchronized(ob){
while(true)
{
System.out.println(Thread.currentThread().getName()+"Count:"+count++);
sleep(1000);
if(isWait){
System.out.println(Thread.currentThread().getName()+"wait");
ob.notify();
ob.wait();
}
}
}
}catch(Exception e){}
}
public void n()
{
notify();
}
public boolean isWait;
private WaitAndNotifyJFrame control;
private int count;
}