Here goes the code
public class MyThread implements Runnable { private boolean stop; @Override public void run() { System.out.println("Thread starts"); while(!stop) { try { //do something fooBar() System.out.println("Sleeping..."); Thread.sleep(5000); } catch(InterruptedException e) { System.out.println("Thread was inturrupted"); } catch(Exception e) { //handle error e.printStackTrace(); } } System.out.println("Thread ends"); } public void setStop(boolean stop) { this.stop = stop; } public static void main(String args[]) throws InterruptedException { MyThread m = new MyThread(); Thread t = new Thread(m); t.start(); System.out.println("Main thread Sleeping..."); Thread.sleep(2000); //stop in this manner m.setStop(true); t.interrupt(); } }
Java has rich concurrency API even though it doesn't provide any safe method to stop a Thread or reuse a Thread directly. Though Executor framework some what handles this by using worker thread but in many scenario a direct control is needed. By the way here is mine way of Stop Thread in Java
ReplyDeleteThe producer–consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue
ReplyDeletehttp://www.youtube.com/watch?v=dUwboVZ59KM
Learn when and How to Stop the Thread,using jconsole or JvisualVM to indetify is the thread is running
http://www.youtube.com/watch?v=3_Bqhw0d2ko
Post is very useful. Thank you, this useful information.
ReplyDeleteLearn Best Microsoft Training in Bangalore from Experts. Softgen Infotech offers the Best Microsoft Training Course.100% Placement Assistance, Live Classroom Sessions, Only Technical Profiles, 24x7 Lab Infrastructure Support.
Great learning experience with proficient trainers in Informatica. Flexible timings with hands on experience with real time scenarios.
ReplyDeletemicrosoft training in bangalore
microsoft training institutes in bangalore
microsoft course content
microsoft training centres in bangalore
microsoft course syllabus
microsoft training
I wanted to build my career in IT. Hats-off to the trainers at the training centre. They provide complete assistance and always ready to guide.
ReplyDeletemicrosoft training and placement support in bangalore
microsoft training free demo class
microsoft placement bangalore
microsoft online training
microsoft classroom training
microsoft training with lab facilities
microsoft training with certified and experienced trainers
Impressive content.Awesome blog.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
A lovely content, Thankyou very much for sharing a great information. Looking forward to more great information. All the best.!
ReplyDeletedata science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai