100分求救:关于wait()和notify()的问题,各位大哥请进!
gull 2002-06-03 10:09:31 我的问题是我想提供一个功能,把ServerThread暂停,我想到了用wait(),可是我用了以后,连主界面都没反映了,大家帮我看看吧!我头发都快急白乐!谢谢!!!!!!!
下面是源程序,是可以直接运行的,大家Copy,paste,运行一下,帮帮忙吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.swing.border.*;
class ClientThread extends Thread{
Socket ClientSocket;
public ClientThread(Socket socket){
ClientSocket=socket;
}
public void run(){
//operations
}
}
class messager{
static void printmsg(String msg){
JOptionPane.showMessageDialog(null,msg,"Message",JOptionPane.INFORMATION_MESSAGE);
}
static void printerr(String msg){
JOptionPane.showMessageDialog(null,msg,"ERROR",JOptionPane.ERROR_MESSAGE);
}
}
class JStatusBar extends JLabel{
private JLabel text=new JLabel();
public final float LEFT=super.LEFT_ALIGNMENT;
public final float RIGHT=super.RIGHT_ALIGNMENT;
public final float CENTER=super.CENTER_ALIGNMENT;
JStatusBar(){
super();
setAlignmentX(LEFT_ALIGNMENT);
setAlignmentY(TOP_ALIGNMENT);
setBorder(new BevelBorder(BevelBorder.LOWERED));
}
public void show(String msg){
setText(msg);
}
public void setAlignment(float pos){
setAlignmentX(pos);
}
}
class Tcpserver{
private static ServerThread server;
//main Frame components
private static JFrame mainFrame=new JFrame();
private static JTextArea textarea=new JTextArea();
private static JScrollPane jsp=new JScrollPane(textarea);
private static JMenuBar menubar=new JMenuBar();
private static JMenu mnuOperations=new JMenu("Operations");
private static JStatusBar statusbar=new JStatusBar();
//Menus
private static JMenuItem mnuStart=new JMenuItem("Start Server");
private static JMenuItem mnuStop=new JMenuItem("Stop Server");
private static JMenuItem mnuSettings=new JMenuItem("Settings");
private static JMenuItem mnuBroadcast=new JMenuItem("Broadcast...");
private static JMenuItem mnuExit=new JMenuItem("Exit");
//Actions for the menu
private static Action actStart=
new AbstractAction("Start Server"){
public void actionPerformed(ActionEvent e){
//operations to start
}
};
private static Action actStop=
new AbstractAction("Stop Server"){
public void actionPerformed(ActionEvent e){
//operations to stop
server.Stop();
}
};
private static Action actSettings=
new AbstractAction("Settings"){
public void actionPerformed(ActionEvent e){
//operations to lauch the settings dialog
}
};
private static Action actExit=
new AbstractAction("Exit"){
public void actionPerformed(ActionEvent e){
//operations to close
mainFrame.dispose();
System.exit(0);
}
};
private static Action actBroadcast=
new AbstractAction("Broadcast..."){
public void actionPerformed(ActionEvent e){
//operations to broadcast msg
}
};
//methods
public static void showMessage(String msg){
textarea.append(msg+"\n");
}
public static void showStatus(String msg){
statusbar.show(msg);
}
public static void clear(){
textarea.setText("");
}
public static void main(String args[]) throws Exception{
//initilize the main frame
mainFrame.setSize(300, 400);
mainFrame.setTitle("Tcpserver");
mainFrame.getContentPane().add(jsp);
mainFrame.getContentPane().add(statusbar,BorderLayout.SOUTH);
menubar.add(mnuOperations);
mainFrame.setJMenuBar(menubar);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
//setup menu actions
mnuStart.setAction(actStart);
mnuStop.setAction(actStop);
mnuSettings.setAction(actSettings);
mnuBroadcast.setAction(actBroadcast);
mnuExit.setAction(actExit);
//add menu items to menu
mnuOperations.add(mnuStart);
mnuOperations.add(mnuStop);
mnuOperations.addSeparator();
mnuOperations.add(mnuSettings);
mnuOperations.add(mnuBroadcast);
mnuOperations.addSeparator();
mnuOperations.add(mnuExit);
//create a server thread and start to listen
server=new ServerThread();
server.start();
}
}
class ServerThread extends Thread{
private final int PORT=1020;
private ServerSocket LocalServerSocket;
public synchronized void Stop(){
try{
wait();
}
catch(Exception e){
messager.printerr(e.getMessage());
}
}
public void run(){
//setup the server socket and
//waiting for client connections
try{
LocalServerSocket=new ServerSocket(PORT);
Tcpserver.clear();
Tcpserver.showMessage("Server Started.");
Tcpserver.showStatus("Server Started.");
Tcpserver.showMessage("Listening to port:1020...");
//listening to the port
//block if no connection requested
while(true){
//spawn a child thread
new ClientThread(LocalServerSocket.accept());
}
}
catch(IOException ioe){
messager.printerr("IO Exception Detected:"+ioe.getMessage());
messager.printerr("The Application is now terminated.");
System.exit(1);
}
}
}