| 1 | import java.util.concurrent.ThreadLocalRandom; |
| 2 | |
| 3 | public class ThreadClientSend implements Runnable { |
| 4 | private SocketClient client; |
| 5 | private String msg = new String(); |
| 6 | |
| 7 | ThreadClientSend(SocketClient c) { |
| 8 | client = c; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Message to sent setter |
| 13 | * @param m the text message to sent |
| 14 | */ |
| 15 | public void setMsg(String m) { |
| 16 | msg = m; |
| 17 | } |
| 18 | |
| 19 | public void run() { |
| 20 | while (true) { |
| 21 | try { |
| 22 | client.sendMsg(msg); |
| 23 | System.out.println (Thread.currentThread().getName() + " a envoye " + msg); |
| 24 | try { |
| 25 | Thread.sleep(ThreadLocalRandom.current().nextInt(101)); |
| 26 | } |
| 27 | catch (InterruptedException e) { |
| 28 | System.err.println("InterruptedException: " + e); |
| 29 | } |
| 30 | } |
| 31 | catch (Exception e) { |
| 32 | System.err.println("Exception: " + e); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |