Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
vandzi
Posts: 40
Registered: ‎07-21-2009
My Device: ...............
Accepted Solution

Thread timeout

Hello, Is it possible to set timeout to the thread? Or kill thread if it's working longer than expected?If yes, how?  Thanks

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Thread timeout

"Is it possible to set timeout to the thread?"

 

Not that I am aware of.

 

"Or kill thread if it's working longer than expected?If yes, how?"

 

Start another Thread or a Timer, giving it a reference to the Thread to be killed.  When the timeout period expires check to see if the Thread has completed (isAlive()) and if so attempt to kill it.  How you kill it will depend on what it is doing, you can try Thread.interrupt() but I am not sure that this will break a http connection for example. 

Please use plain text.
Developer
vandzi
Posts: 40
Registered: ‎07-21-2009
My Device: ...............

Re: Thread timeout

Thanks. I am trying to kill thread which is sending sms, because if there is no network threre is nothing what invoke me that sms can not be send. It's running to infinity.

 

I am trying it with this code

Thread killerThread = new Thread(new Runnable() {

			public void run() {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(smsThread.isAlive()){
					smsThread.interrupt();
				}
			}
		});

killerThread.start();
smsThread.start();

 

 

 

Please use plain text.