当前位置: 技术问答>java相关
100分求救:关于wait()和notify()的问题,各位大哥请进!
来源: 互联网 发布时间:2015-06-14
本文导语: 我的问题是我想提供一个功能,把ServerThread暂停,我想到了用wait(),可是我用了以后,连主界面都没反映了,大家帮我看看吧!我头发都快急白乐!谢谢!!!!!!! 下面是源程序,是可以直接运行的,大家Copy,paste...
我的问题是我想提供一个功能,把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);
}
}
}
下面是源程序,是可以直接运行的,大家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);
}
}
}
|
还是用设置标志位关闭吧:
设置class ServerThread extends Thread{
private boolean stoped=false;
.......
public void stop(){stoped=true};//添加方法
public void run(){
try{
LocalServerSocket=new ServerSocket(PORT);
Tcpserver.clear();
......
while(!stoped){//监测标志位
//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);
}finally{
LocalServerSocket.close();//加入
}
}
这样你就可以在main()里调用ServerSocket.stop()来终止了
设置class ServerThread extends Thread{
private boolean stoped=false;
.......
public void stop(){stoped=true};//添加方法
public void run(){
try{
LocalServerSocket=new ServerSocket(PORT);
Tcpserver.clear();
......
while(!stoped){//监测标志位
//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);
}finally{
LocalServerSocket.close();//加入
}
}
这样你就可以在main()里调用ServerSocket.stop()来终止了
|
你可以试一试sleep()
|
这个应该很好办吧!你的LocalServerSocket是在主线程吧?
while(true) {
Socket s=LocalServerSocket.accept();
}
你说的你的线程被block后,你不想要这个Server了?你说的是s还是LocalServerSocket?如果是s呀,我就建议你为打开的服务建立一个线程,把这个线程关闭就可以了.
while(true) {
Socket s=LocalServerSocket.accept();
}
你说的你的线程被block后,你不想要这个Server了?你说的是s还是LocalServerSocket?如果是s呀,我就建议你为打开的服务建立一个线程,把这个线程关闭就可以了.
|
太长了,难得看啊
|
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupupupupupup
upupup upupupupupup upupupupupupupupupupup upupup
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupupupupupup
upupup upupupupupup upupupupupupupupupupup upupup
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup
|
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupupupupupup
upupup upupupupupup upupupupupupupupupupup upupup
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupup upupup
upupup upupupupupupup upupupupupup upupupupupupupupupupupupupupup
upupup upupupupupup upupupupupupupupupupup upupup
upupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupupup