当前位置:  技术问答>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);
}
}
}

|
还是用设置标志位关闭吧:
设置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呀,我就建议你为打开的服务建立一个线程,把这个线程关闭就可以了.

|
太长了,难得看啊

|
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

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 求救!求救!紧急求救!为什么更新不了所指定的内容?
  • 求救!!!硬件高请进、、、、、、(十万火急,高分求救。)
  • 求救求救!!
  • 求救啊 高分求救 UNIX下关于进程通讯的问题~
  • 求救!!!求救!!!机器不能正常启动
  • 关于jdbc,求救求救!在线等待,马上给分
  • 紧急求救,root用户无权限删除文件
  • 晕,特晕...求救...
  • 高分求救~~如何取得linux下进程完整命令行字符串,就是的ps -ef 完整的全路径的CMD那一列,求救!!!!附现在的代码
  • 求救!weblogic6.0后台运行正确,前台页面跳转或调用其他页面时出“页面无法显示错误”
  • 求救:java里如何取整一个浮点数(不做四舍五入)
  • 紧急求救!!
  • Linux下无法启动apache 高分求救!在线等待
  • 紧急求救 我用freebsd通过smbfs连接win2000的一些问题 (分不够可加)
  • 紧急求救 我用freebsd通过smbfs连接win2000的一些问题
  • 求救,linux和windows之间如何联成局域网(设置),并且相互之间移动文件。
  • 求救!!在Redhat7.3下安装scim0.9.3怎么安装?
  • Linux8.0 修改字符集后,再次进系统,无图形界面问题。。。求救。。
  • SUSE网络打印机问题,在线等,求救!!
  • 散分一百,紧急求救!ROOT密码忘记


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3