简体   繁体   English

Java-为不同任务创建线程

[英]Java - creating threads for different tasks

public class server implements Runnable {

private static final int initialize_server_Port = 8080 ;
private static final int store_server_Port =  8181;
private static final int search_server_Port = 8282;
private static String tname ;

private static InetAddress address ;

protected static Hashtable<String , InetAddress> file_location = new Hashtable<String ,InetAddress>();
server(String tname){
    this.tname = tname ;
}

public void run(){

   try{
    if("storeRecords".equals(tname))
        storeRecord();}catch(IOException e ){
            System.out.println("error: unable to create store socket");
        }
    try{
        if("searchRecords".equals(tname))
        searchRecord();}catch(IOException e){
            System.out.println(e);


        }

   // if("search".equals(tname))
     //   searchRecord();

}
public void start(){
   Thread thread = new Thread(this , tname );
    thread.start();
}
public static void storeRecord() throws IOException{
    System.out.println("store out");
    DatagramSocket serverSocket = new DatagramSocket(store_server_Port);
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];

    while(true){

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    String file = new String( receivePacket.getData());
        file = file.trim();
        System.out.println("RECEIVED: " + file);
    InetAddress IPAddress = receivePacket.getAddress();
        address = IPAddress;
    int port = receivePacket.getPort();
        System.out.println(file);
        file_location.put(file , IPAddress);
    String confirmation= "Successfully uploaded";
    sendData = confirmation.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
    serverSocket.send(sendPacket);


    }

}

public static void searchRecord() throws IOException{

    System.out.println("search out");
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];
    DatagramSocket serverSocket = new DatagramSocket(search_server_Port);
    while(true){

        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String file = new String( receivePacket.getData());
        file = file.trim();
        System.out.println("RECEIVED: " + file);
        InetAddress IPAddress = receivePacket.getAddress();
        address = IPAddress;
        int port = receivePacket.getPort();
        boolean found = file_location.containsKey(file);
        if(found == true ){
      InetAddress query =  file_location.get(file);
        String confirmation= "found";
        sendData = confirmation.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);}
        else if (found != true){
            String confirmation= "404";
            sendData = confirmation.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
        }



    }
}



protected static String server_IP ;

public static void main(String[] args) throws IOException {

   server storeThread = new server("storeRecords"); //**************
    storeThread.start();
    server searchThread = new server("searchRecords");
    searchThread.start(); //*********************************
       //     PrintWriter pw = null ;
//Socket socket = null ;

    try {
        InetAddress iAddress = InetAddress.getLocalHost();
        server_IP = iAddress.getHostAddress();
        System.out.println("Server IP address : " +server_IP);
    } catch (UnknownHostException e) {
    }

    DatagramSocket serverSocket = new DatagramSocket(initialize_server_Port);
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];


        while (true) {
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            System.out.println("RECEIVED: " + sentence);
            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();
            String confirmation= "Successfully initialized";
            sendData = confirmation.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);

        }



}
}

So right now the output is 所以现在的输出是

 search out
 search out

what im trying to do is create three threads, one thread running the main, 2nd thread running storeRecord which should printout Store out and 3rd running searchRecord , what am i doing wrong here? 我想做的是创建三个线程,一个线程运行主线程,第二个线程运行storeRecord(应打印输出),第三个运行searchRecord,我在这里做错了什么?

In your code, tname is a static variable, hence, shared by all instances of server . 在您的代码中, tname是静态变量,因此由server的所有实例共享。 It is assigned twice in two calls to the constructor. 它在两次调用构造函数中分配了两次。 Making it an instance variable (ie, removing the static) should do what you want. 使它成为实例变量(即删除静态变量)应该可以完成您想要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM