JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區 » Threads/Synchronization  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 使用thread時.呼叫別的方法.被呼叫的方法寫在哪有差嗎?
sbphsho





發文: 15
積分: 0
於 2008-03-19 11:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
以下是我寫的一個簡單的server端程式,對於thread的用法我有個問題,就是在實做runnable時改寫run()時,如果我在run()中呼叫了另一個方法setType(),這個方法寫在『實做runnable的類別中』和『另外寫一個類別中』,又如果我不另外寫setType的方法,直接把我要執行的動作寫在run()中,在執行上得流程跟效能有什麼差別?大家又都是怎麼寫的呢?

程式碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class DataServer {
    public static void main(String[] args) throws Exception ,IOException{
      .
      .
      .(其他初始化的動作)
    server.startServer(Integer.parseInt(props.getProperty("port")));   
  }
  
  
  private void startServer(int port) {
    try {
      ss = new ServerSocket(port);
      Thread thread = new Thread(new ClientThread(ss));
      thread.start();
  }
    catch (IOException ioe) {
      ioe.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
class ClientThread implements Runnable {
  private ServerSocket ss;
  private Socket cs;
  public ClientThread(ServerSocket ss) throws Exception {
    this.ss = ss;
  }
/* public void setType(int i) {
    .
    .
  } (寫在實做Runnable的ClientThread類別中)*/
  public void run() {
    try {
      while(true) {
        cs = ss.accept();
        System.out.println("Connection from " +  cs.getInetAddress().getHostAddress());
        DataInputStream in = new DataInputStream (cs.getInputStream());
       
        setType(in.readInt());
 
        //Type type = new type();
        //type.setType(in.readInt());
 
        in.close();
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
 
=======================================
/*pubilc class Type{
           public void setType(int i) {
    .
    .
  } 
}(寫在另外的類別中)*/


sbphsho edited on 2008-03-19 12:58
reply to postreply to post
作者 Re:使用thread時.呼叫別的方法.被呼叫的方法寫在哪有差嗎? [Re:sbphsho]
hkdennis2k





發文: 1926
積分: 6
於 2008-03-19 23:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
沒差得好少

倒是設計, 元件化, 易讀性, debug 難度, unit test 易不易等等地方會差異很多


reply to postreply to post
1. Programming is not coding

2. Learning Java is not leaning Java syntax

3. Study, not wait for be taught

4. Answer to yourself, not ask somebody else

5. Code is poetry

---
6. 我跑去寫 C# 了....

---
7. 回到 Java, PHP 還有 servers farm
---
8. 很久沒有寫 Java 了, 倒
» JWorld@TW »  Java SE 討論區 » Threads/Synchronization

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8