为什么客户端线程仅执行一次后会被销毁? Java TCP套接字 [英] Why client thread is destroyed after it is executed only once? Java TCP sockets

查看:83
本文介绍了为什么客户端线程仅执行一次后会被销毁? Java TCP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的客户端类运行一个线程,该线程每5秒向服务器连续发送一次String信息(顺序).但是取而代之的是,线程在发送第一个命令后被销毁.我不知道为什么以及如何阻止它,有人可以帮助我吗?

I want my client class to run a thread that sends String information (order) to the server continuously every 5 sec. But instead thread is destroyed after it sends first order. I dont know why and how to stop it, can somebody help me?

下面的代码;

public class Cashier implements Runnable
{
    private static final int MAX_DELAY = 5000;
    static OrderList orderList;
    static Socket socket;
    static PrintWriter out = null;
    static BufferedReader in = null;

    int orderNumber = 0;    
    public String order, strorderNumber;

    public static void main(String[] args){


        Cashier newCashier = new Cashier();
        Thread cashierThread = new Thread(newCashier);
        cashierThread.setName("Olaf");
        cashierThread.setDaemon(false);
        cashierThread.start();

        try {
            socket = new Socket("127.0.0.1", 4444);
             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             out = new PrintWriter(socket.getOutputStream(), true);

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run()
    {
            try{
                Date now = new Date();
                Format fTime = new SimpleDateFormat("hh:mm:ss a");
                order = ("Order made by " + Thread.currentThread().getName()+ " at " + fTime.format(now)+ "\n");

                out.print(order);

                Random randomNumber = new Random();
                Thread.sleep(randomNumber.nextInt(MAX_DELAY));
                } catch (InterruptedException exception){       
                    System.out.println("Olafs interrupted exception");
    }

    }
}

推荐答案

您的课程的run方法不包含循环,因此它将执行它必须做的事情一次然后跌倒从底部开始,有效地终止线程.

The run method for your class does not contain a loop, so it will do what it has to do once then fall out the bottom, effectively terminating the thread.

如果要使其连续运行,则需要类似以下内容的东西:

If you want it to run continuously, you'll need something like:

public void run() {
    boolean keepGoing = true;
    while (keepGoing) {
        try {
            Date now = new Date();
            Format fTime = new SimpleDateFormat("hh:mm:ss a");
            order = "Order made by " + Thread.currentThread().getName()
                + " at " + fTime.format(now)+ "\n";

            out.print(order);

            Random randomNumber = new Random();
            Thread.sleep(randomNumber.nextInt(MAX_DELAY));
        } catch (InterruptedException exception){       
            System.out.println("Olafs interrupted exception");
            keepGoing = false;
        }
    }
}

这篇关于为什么客户端线程仅执行一次后会被销毁? Java TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆