使用套接字的Java中的Multhithreading

时间:2019-07-03 12:29:58

标签: java multithreading server serversocket lan

我的任务是创建一个可通过LAN网络模拟打印机的程序。该程序中有3个队列,描述3台可用打印机。每个队列稍后都会有一个线程。当客户端连接到服务器时,它将循环播放,直到客户端输入相同的文本为止。 当客户端将文本发送到服务器进行打印时,系统会将文本输入到最短的队列中。我不确定thread.start方法在队列中的位置。如果我将其放置在服务器类中,则队列的元素将不会更改,导致在ClientHandler类中添加要打印的文本。如果将其放在ClientHandler类中,则该队列元素仅适用于该特定客户端。到目前为止,这是我的代码。它只是在 setArray 方法之后卡住了。

Server.java

public class Server{
    public static void main(String[] args) throws IOException  {                 
        ArrayList<Printer> queue = new ArrayList<>();
        queue.add(new Printer(1));
        .........       
        ServerSocket ss = new ServerSocket(5056);         
        while (true){ 
            Socket s = null;             
            try { 
                s = ss.accept();                   
                Runnable ch = new ClientHandler(s,queue);                
                new Thread(ch).start();                
            }catch (Exception e){ 
               .....
            } 
        } 
    }
} 

ClientHandler.java

class ClientHandler implements Runnable{     
    final DataInputStream dis; 
    final DataOutputStream dos; 
    final Socket s; 
    ArrayList<Printer> queue;
    //constructor
    @Override
    public void run(){ 
        String received;                 
        Thread t1 = new Printer(queue.get(0).getid(),queue.get(0).getQueue());                
        t1.start();
        //and the other queue
        while (true){ 
            try { 
                received = dis.readUTF();
                if(received.equals("Exit")){  
                  //exit 
                } 
                Message msg = new Message(received,this.s);                
                Printer pr = queue.get(0);  //example the first queue selected
                pr.Addqueue(msg);
                setArray(pr);
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        }
    } 
    public synchronized void setArray(Printer pr){
      this.queue.set(0,pr);  //add new request to queue
    }
}

Printer.java

class Printer extends Thread{
    private int id;    
    private LinkedList queue = new LinkedList();   
    public synchronized void Addqueue(Message ps){
      this.queue.addLast(ps);
    }
    public synchronized void delfirst(){
      this.queue.remove(0);
    }
    @Override
    public void run(){ 
        String received; 
        while(true){
            try{                  
                while(this.queue.size()!=0){  //process if queue not null
                    Message ps = (Message)this.queue.get(0);
                    Socket l = ps.getSocket();
                    received = ps.getMessage();
                    DataOutputStream dos = new DataOutputStream(l.getOutputStream()); 
                    dos.writeUTF("Printing "+received);                                                              
                    thread.sleep(1000);
                    this.delfirst();
                }
            } catch (IOException e) { 
                e.printStackTrace();          
            } 
        }
    }
}

Message.java

public class Message {
private String message;
private Socket s;    
      //contrustor and getter setter
}

请帮我解决这个问题。谢谢

0 个答案:

没有答案