0

我的程序是每当我启动我的 AccelSender 程序时,它都会将 epnId 发送到我的服务器机器以请求该特定 epnId 的主机。当服务器返回 hostName 时,它​​会启动两个 Runnable Thread 类,名为new DataTransmitter(hostName,epnId)和 new JMSConnection()

我想要做的是每当我reStart(String hostName)被调用时,我想停止new DataTransmitter(hostName,epnId)线程并通过设置新的主机名来启动它。

这是我的代码:

public class AccelSender {

        private Socket kkSocket = null;
        private PrintWriter out = null;
        private BufferedReader in = null;

        private static final String epnId = "EPN1";

        public AccelSender(){
        }

        public void requestHost(){

            try{
                Socket hostSocket = new Socket("10.3.2.227",1121);
                PrintWriter out = new PrintWriter(hostSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(hostSocket.getInputStream()));
                out.println(epnId);
                    while(true){
                        String hostName = in.readLine();
                        DataTransmitter dt = new DataTransmitter(hostName,epnId);
                        JMSConnection jms = new JMSConnection();
                        new Thread(dt).start();
                        new Thread(jms).start();
                    }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

    public void reStart(String hostName){
    //Here i want to STOP the DataTransmitter Thread and,
 START with new hostName
        }

    }

Runnable 类的代码:

public class DataTransmitter implements Runnable {

    private Socket kkSocket = null;
    private PrintWriter out = null;
    private BufferedReader in = null;

    private int port = 2508, fileCount = 0, arrivalRate = 500;
    private String hostName = null, data = null, filename = null;
    private boolean resetSender = false;
    private static String epnId = null;
    File folder = new File(System.getProperty("user.dir")+"/input_data");
    File[] listOfFiles = folder.listFiles();

    public DataTransmitter(){
    }


    public DataTransmitter(String hostName, String epnId){

        this.hostName = hostName;
        this.epnId = epnId;
        establishHostConnection();
    }

    public void establishHostConnection(){

        try {
            kkSocket = new Socket(hostName, port);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new 
InputStreamReader(kkSocket.getInputStream()));
            resetSender = true;
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: thinklatch.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 
thinklatch.");
            System.exit(1);
        }
    }

    public void close() throws IOException{

        kkSocket.close();
        out.close();
        in.close();
    }

    public void run(){

        System.out.println("Entering Data Transmitter");

        try{
            while (fileCount <= 1) {


                for (int i = 0; i < listOfFiles.length; i++) {
                    if (listOfFiles[i].isFile()) {
                        System.out.println("File " + 
listOfFiles[i].getName());
                        filename = System.getProperty("user.dir") + 
"/input_data/" + listOfFiles[i].getName();

                        BufferedReader bf = new BufferedReader(new 
FileReader(filename));
                        System.out.println("Fetching input from : " + 
filename);

                        while((data=bf.readLine())!=null){
                            String str = 
this.hostName+","+this.epnId+","+arrivalRate+","+data;
                            out.println(str);
                            try{
                                TimeUnit.MILLISECONDS.sleep(2);
                            }catch(Exception e){
                            }
                        }
                    }
                }
                     fileCount++;
            }
        }catch(FileNotFoundException fnfe){
            System.err.println("E"+fnfe);
        }catch(IOException ioe){
            System.err.println("E"+ioe);
        }
            out.close();
    }
}

针对这种情况重新启动线程的任何建议都是可观的。提前致谢 ....

4

3 回答 3

4

run一旦方法完成执行,线程实际上就不能“重新启动” 。你基本上有两个选择:

  1. 引入一个循环,在满足条件时导致线程被挂起(例如调用 a wait)。这样,您可以“暂停”线程,更改主机名,然后发出信号以恢复。
  2. 使线程完成其代码,丢弃它,然后使用相同的Runnable对象启动一个新线程。

编辑:所以你可以在重启方法中做的是调用对象close上的方法。dt这将关闭套接字并导致IOException线程退出其运行方法。之后,您可以开始一个新线程:

dt = new DataTransmitter(hostName,epnId);
new Thread(dt).start();
于 2012-09-18T07:26:07.327 回答
1

使用 Thread.currentThread().isInterrupted() 和 Thread.interrupt()

请参阅此链接:

http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

于 2012-09-18T07:23:12.263 回答
0

Thread一旦停止can't be restarted

-一旦与线程类实例关联的执行线程完成它的run()方法,它就会移动到Dead State,或者Thread Pool如果已实现。与执行线程关联的线程类实例永久失去其线程性。

-你可以更好地使用wait()andnotify()机制,也可以使用java.util.concurrent包中的await()andsignal()机制来挂起和恢复线程。

于 2012-09-18T07:37:11.553 回答