2016-02-27 61 views
0

這是包含main()方法的類:什麼是run()函數在ProxyThread類中如果不被任何地方調用?

public class MultithreadedProxyServer { 

    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 

     int port = 10000; //default 
     try { 
      port = Integer.parseInt(args[0]); 
     } catch (Exception e) { 
      //ignore me 
      System.out.println("gnore"); 
     } 

     try { 
      serverSocket = new ServerSocket(port); 
      System.out.println("Started on: " + port); 
     } catch (IOException e) { 
      System.err.println("Could not listen on port: " + args[0]); 
      System.exit(-1); 
     } 

     while (listening) { 
      new ProxyThread(serverSocket.accept()).start(); 
     } 
     serverSocket.close(); 
    } 
} 

這是ProxyThread類:

public class ProxyThread extends Thread { 


    private Socket socket = null; 
    private static final int BUFFER_SIZE = 32768; 

    public ProxyThread(Socket socket) { 
     super("ProxyThread"); 
     this.socket = socket; //initialzed my parent before you initalize me 

    } 

    public void run() { 
     //get input from user 
     //send request to server 
     //get response from server 
     //send response to user 
     System.out.println("run"); 

     try { 
      DataOutputStream out = 
       new DataOutputStream(socket.getOutputStream()); 
      BufferedReader in = new BufferedReader(
       new InputStreamReader(socket.getInputStream())); 

      String inputLine, outputLine; 
      int cnt = 0; 
      String urlToCall = ""; 
      /////////////////////////////////// 
      //begin get request from client 
      while ((inputLine = in.readLine()) != null) { 
       try { 
        StringTokenizer tok = new StringTokenizer(inputLine); 
        tok.nextToken(); 
       } catch (Exception e) { 
        System.out.println("break"); 
        break; 
       } 
       //parse the first line of the request to find the url 
       if (cnt == 0) { 
        String[] tokens = inputLine.split(" "); 
        urlToCall = tokens[1]; 
        //can redirect this to output log 
        System.out.println("Request for : " + urlToCall); 
       } 

       cnt++; 
      } 
      //end get request from client 
      /////////////////////////////////// 


      BufferedReader rd = null; 
      try { 
       //System.out.println("sending request 
       //to real server for url: " 
       //  + urlToCall); 
       /////////////////////////////////// 
       //begin send request to server, get response from server 
       URL url = new URL(urlToCall); 
       URLConnection conn = url.openConnection(); 
       conn.setDoInput(true); 
       //not doing HTTP posts 
       conn.setDoOutput(false); 
       //System.out.println("Type is: " 
       //+ conn.getContentType()); 
       //System.out.println("content length: " 
       //+ conn.getContentLength()); 
       //System.out.println("allowed user interaction: " 
       //+ conn.getAllowUserInteraction()); 
       //System.out.println("content encoding: " 
       //+ conn.getContentEncoding()); 
       //System.out.println("content type: " 
       //+ conn.getContentType()); 

       // Get the response 
       InputStream is = null; 
       HttpURLConnection huc = (HttpURLConnection)conn; 
       if (conn.getContentLength() > 0) { 
        is = conn.getInputStream(); 
        rd = new BufferedReader(new InputStreamReader(is)); 
       } 
       //end send request to server, get response from server 
       /////////////////////////////////// 

       /////////////////////////////////// 
       //begin send response to client 
       byte by[] = new byte[ BUFFER_SIZE ]; 
       int index = is.read(by, 0, BUFFER_SIZE); 
       while (index != -1) 
       { 
        out.write(by, 0, index); 
        index = is.read(by, 0, BUFFER_SIZE); 
       } 
       out.flush(); 

       //end send response to client 
       /////////////////////////////////// 
      } catch (Exception e) { 
       //can redirect this to error log 
       System.err.println("Encountered exception: " + e); 
       //encountered error - just send nothing back, so 
       //processing can continue 
       out.writeBytes(""); 
      } 

      //close out all resources 
      if (rd != null) { 
       rd.close(); 
      } 
      if (out != null) { 
       out.close(); 
      } 
      if (in != null) { 
       in.close(); 
      } 
      if (socket != null) { 
       socket.close(); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我有複製粘貼從互聯網上面的代碼,但是我有運行困難它。

+0

您能否介紹一下您遇到的困難? –

回答

0

要回答帖子標題中的問題,JVM會調用ProxyThread類中的run()方法,在線程啓動後new ProxyThread(serverSocket.accept()).start();通常包含線程應執行的實際工作(在本例中,它處理無論服務器套接字收到什麼,它都接受來自客戶端的連接)。 JVM調用run()方法的時刻不能由程序員控制,而是在線程啓動後。

run()方法永遠不會被程序員顯式調用。

相關問題