当前线程本身在尝试调用另一个线程上的 wait() 时无限等待

我试图让另一个线程在下面的代码中等待,但我当前的线程本身正在无限等待。下面是两个 Java 类 Server.java,它们生成 ServerService.java 的可运行实例。当“ServerService.java”的这种运行实例调用Server.java.Server.java的入队方法时,应该让这样的被调用线程等待。但似乎我的 Server.java 线程本身无限等待


服务器端.java

import java.io.DataInputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Queue;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


public class Server{

    private ServerSocket server=null;

    public static Map<Socket,String> clientsConnected=null;

    public static Map<Socket,Runnable> clientsAndThreads=null;

    public static ExecutorService executor=null;

    public static List<Runnable> requestQueue=null;

    public static Map<Runnable,Integer> threadAndRespectiveTime=null;

    /*

     * Contructor

     */

    Server(){

        clientsConnected=new HashMap<Socket,String>();

        clientsAndThreads=new HashMap<Socket,Runnable>();

        threadAndRespectiveTime=new HashMap<>();

        requestQueue=new ArrayList<>();

    }


下面是由 Server.java 作为线程产生的类



侃侃无极
浏览 151回答 1
1回答

杨__羊羊

My ServerService 线程实例在调用 Server.java 的 enqueue 方法后等待自身,而不是在产生的线程上调用 wait。然后Server.java 调用notify 来恢复ServerService 线程。服务器端.javaimport java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Queue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.SynchronousQueue;public class Server{&nbsp; &nbsp; private ServerSocket server=null;&nbsp; &nbsp; public static Map<Socket,String> clientsConnected=null;&nbsp; &nbsp; public static Map<Socket,Runnable> clientsAndThreads=null;&nbsp; &nbsp; public static ExecutorService executor=null;&nbsp; &nbsp; public static Queue<Thread> requestQueue=null;&nbsp; &nbsp; public static Map<Thread,Integer> threadAndRespectiveTime=null;&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Contructor&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; Server(){&nbsp; &nbsp; &nbsp; &nbsp; clientsConnected=new HashMap<Socket,String>();&nbsp; &nbsp; &nbsp; &nbsp; clientsAndThreads=new HashMap<Socket,Runnable>();&nbsp; &nbsp; &nbsp; &nbsp; threadAndRespectiveTime=new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; requestQueue=new LinkedList<>();&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Accepts connections from clients continually till the server is UP(max 10 clients)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void acceptConnection(){&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executor=Executors.newFixedThreadPool(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Thread(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Socket client=null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(server.isBound()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client=server.accept();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataInputStream di= new DataInputStream(client.getInputStream());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String msg=di.readUTF();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clientsConnected.put(client, getMessage(msg));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerWindow.write(msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Runnable service= new ServerService(client,getMessage(msg));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executor.execute(service);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("error occurred while accepting connections");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Thread(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Server.dequeue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).start();&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Server:error while accepting connections"+e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static synchronized void enqueue(Thread t,Integer secondsToWait){&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(requestQueue );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadAndRespectiveTime.put(t, secondsToWait);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestQueue.add(t);&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static synchronized void dequeue() throws InterruptedException{&nbsp; &nbsp; &nbsp; &nbsp; while(!requestQueue.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread currentThread=requestQueue.remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer timeToWait=threadAndRespectiveTime.get(currentThread);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("time to wait is:"+timeToWait);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.currentThread().sleep(timeToWait * 1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (currentThread) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentThread.notify();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This method takes out actual message from http format&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String getMessage(String str){&nbsp; &nbsp; &nbsp; &nbsp; return str.substring(str.indexOf("message:")+8, str.length());&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Starts the server listening to port 4000&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void start_server(){&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(server==null || !server.isBound()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; server = new ServerSocket(4000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acceptConnection();&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Server:error occurred while server start"+e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Closes client sockets of every connected client, shuts down the thread executor that serves clients&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void stop_server() throws IOException{&nbsp; &nbsp; &nbsp; &nbsp; Iterator it=clientsConnected.entrySet().iterator();&nbsp; &nbsp; &nbsp; &nbsp; while(it.hasNext()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map.Entry e= (Map.Entry)it.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Socket toBeClosed=(Socket)e.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toBeClosed.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; executor.shutdownNow();&nbsp; &nbsp; &nbsp; &nbsp; server.close();&nbsp; &nbsp; }}服务器服务.java==================&nbsp; &nbsp; import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.util.Date;/*&nbsp;* This class serves the client&nbsp;*/public class ServerService extends Server implements Runnable{&nbsp; &nbsp; private Socket client=null;&nbsp; &nbsp; private String clientBeingServed=null;&nbsp; &nbsp; private DataOutputStream dout=null;&nbsp; &nbsp; private DataInputStream din=null;&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This is construcor that takes client sockte that already has been connected to server and client name.&nbsp; &nbsp; &nbsp;* It initializes and input and output streams for serving the respective client&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public ServerService(Socket client,String name) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; this.client=client;&nbsp; &nbsp; &nbsp; &nbsp; this.clientBeingServed=name;&nbsp; &nbsp; &nbsp; &nbsp; dout=new DataOutputStream(client.getOutputStream());&nbsp; &nbsp; &nbsp; &nbsp; din=new DataInputStream(client.getInputStream());&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* takes out actual message sent by client from its http format&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String getMessage(String str){&nbsp; &nbsp; &nbsp; &nbsp; //System.out.println("returning\n"+str.substring(str.indexOf("message:")+8, str.length()));&nbsp; &nbsp; &nbsp; &nbsp; return str.substring(str.indexOf("message:")+8, str.length());&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This method converts a message string into HTTP formatted string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String getHttpMessage(String msg){&nbsp; &nbsp; &nbsp; &nbsp; String str="POST Http/1.1 \n" + "Host: www.uta.com \n" + "User-Agent: Mozilla/5.0 \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "Content=type: application/x-www-form-urlencoded \n" + "Content-Length: " + msg.length() + " \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "Date:" + new Date() + " \n" + "message:" + msg;&nbsp; &nbsp; &nbsp; &nbsp; return str;&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This method execute when thread for this class is executed from Server.java file after connection is accepted&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; int waitTime=0;&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(client.isConnected()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String msg=din.readUTF();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerWindow.write(msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; waitTime=Integer.parseInt(getMessage(msg));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Equeing:"+clientBeingServed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Server.enqueue(Thread.currentThread(), waitTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("before going to sleep");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (Thread.currentThread()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.currentThread().wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("after sleeping");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerWindow.write("Served client:"+clientBeingServed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dout.writeUTF(getHttpMessage("Server waited "+waitTime+" seconds for "+clientBeingServed));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dout.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.close();&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("ServerService:error serving client"+clientBeingServed+e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java