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