J'ai crée un Thread qui sert à recevoir les données en provenance d'un client udp ecrit en C.
Je dois réarmer un timer à chaque réception de paquet et je veux que le programme sort au bout de 2s après la réception du dernier paquet.
comment puis je faire?

pour l'instant le timeout réalisé est qu'au bout de 4 seconds le thread se termine.
La classe pour réaliser un timeout est :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
public class Timeout extends TimerTask {
 
		boolean timeout = false;
 
		public void run() {
 
			this.timeout = true; // interrupt execution
			Log.d(TAG,"Thread is up");					
 
		}
	}

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
public class Handler implements Runnable  {
 
		public udp_packet_header header;
		Timer timer;
		byte[] DataOut;
 
		public void run() {
 
			try {
				DataOut = new byte[1024];
				Log.d(TAG, "restart socket");
				socket = new DatagramSocket(SERVER_PORT);
				socket.setReuseAddress(true);
				socket.setBroadcast(true);
				packetIn = new DatagramPacket(DataOut, DataOut.length);
				Log.d(TAG, "waiting for incomming packets ...");
 
				timer = new Timer();
				Timeout timertask = new Timeout();
				timer.schedule(timertask,4000); //4 seconds
 
				while(timertask.timeout == false) {
 
					Log.d(TAG,"bool :" + timertask.timeout);
					Log.d(TAG, "wait message ...");
					socket.receive(packetIn); 
 
	                String comming = "from " + packetIn.getAddress() + ", " +
	                		packetIn.getPort();
 
 
	                Log.d(TAG,""+ comming);
 
	                byte [] data = packetIn.getData();
	                header = new udp_packet_header(data);
 
	                System.out.println(header.ID);
	                System.out.println(header.numberpacket);
	                System.out.println(header.delay_ms);
 
					Log.d(TAG, "receive new packet...");
 
				}// end while
 
				 socket.close();
				 Log.d(TAG, "End of recvthread...");
				 Log.d(TAG,"New Thread started");
 
			} catch (UnknownHostException exc) {
				Log.e(ERROR,"UnknownHostException :",exc);
			} catch (SocketException exc) {
				Log.e(ERROR,"Problem openning socket !",exc);
			} catch (IOException exc) {
				Log.e(ERROR,"Problem in reception of messages : ",exc);
			}catch(IllegalArgumentException i){
				Log.e(ERROR,"IllegalArgumentException : ",i);
			}
 
 
		}
 
	}

Une autre chose qui me gène c'est lorsque je remplace dans ma boucle la condition timertask.timeout == false par true j'obtiens une erreur dans la ligne qui suit la fin de la boucle.