Java - 为不同的任务创建线程

时间:2014-11-20 10:21:48

标签: java multithreading

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);

        }



}
}

现在输出是

 search out
 search out

我想要做的是创建三个线程,一个线程运行main,第二个线程运行storeRecord,应该打印出Store 和第三次运行searchRecord, 我在这做错了什么?

1 个答案:

答案 0 :(得分:1)

在您的代码中, tname 是一个静态变量,因此由 server 的所有实例共享。它在两次调用构造函数时被分配两次。使它成为一个实例变量(即删除静态)应该做你想要的。