views:

134

answers:

3

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?

below code;

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

    }
}
+2  A: 

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;
        }
    }
}
paxdiablo
+1 for the solution and not just pointing out the problem (like me) :P
o.k.w
+1  A: 

Hmm.. there's no loop what-so-ever to perform the 5min interval operation, or am I missing something?

o.k.w
+2  A: 

@paxdiablo has given you a solution to your problem. The answer to your "why" question is in the way that the Thread API works; specifically the Thread.start() method. The javadoc says this:

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

This says that a Thread is started by calling the start method, which calls the thread's run method once. This in turn calls the run method of the Runnable that you supplied as a constructor argument ... once. (If you were to extend the Thread class, and override the Thread.run() method, this second step would typically not happen. But that's a side issue.)

When the Runnable.run() method returns, the Thread.run() method returns and that is it. The thread terminates, its stack is blown away, and you cannot start it again. All you can do is to examine what little remains of the thread state via the Thread object.

Stephen C