एक चल रहे थ्रेड को बाधित करने का प्रयास कर रहा है, इस उदाहरण में, t1, जिसे थ्रेड पूल में थ्रेड द्वारा निष्पादित किया जाता है।एक नींद धागा को बाधित करें
टी 2 वह है जो बाधा भेजता है।
मैं चल रहे टी 1 को रोकने में असमर्थ हूं, टी 1 को इंटरप्टेड एक्सेप्शन नहीं मिलता है।
मुझे क्या याद आ रही है?
Executor exec1 = Executors.newFixedThreadPool(1);
// task to be interrupted
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("starting uninterruptible task 1");
Thread.sleep(4000);
System.out.println("stopping uninterruptible task 1");
} catch (InterruptedException e) {
assertFalse("This line should never be reached.", true);
e.printStackTrace();
}
}
};
final Thread t1 = new Thread(runnable);
// task to send interrupt
Runnable runnable2 = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
t1.interrupt();
System.out.println("task 2 - Trying to stop task 1");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread(runnable2);
exec1.execute(t1);
t2.start();
t2.join();
धन्यवाद BegemoT। यह समझ में आता है। – portoalet
begemot यह सही मिला है –