Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Wrapping a dead thread object in new thread object to restart it
- class Ex13 implements Runnable {
- int i = 0;
- public void run() {
- System.out.println("Running " + ++i);
- }
- public static void main(String[] args) throws Exception {
- Thread th1 = new Thread(new Ex13(), "th1");
- th1.start();
- //th1.join()
- Thread th2 = new Thread(th1);
- th2.start();
- }
- }
- public void run() {
- if (target != null) {
- target.run();
- }
- }
- public static class Ex13 implements Runnable {
- AtomicInteger i = new AtomicInteger(0);
- CountDownLatch latch;
- Ex13(CountDownLatch latch) {
- this.latch = latch;
- }
- public void run() {
- System.out.println("Running " + i.incrementAndGet());
- latch.countDown();
- }
- }
- public static void main(String[] args) throws Exception {
- CountDownLatch latch = new CountDownLatch(2);
- Ex13 r = new Ex13(latch);
- Thread th1 = new Thread(r, "th1");
- th1.start();
- Thread th2 = new Thread(r);
- th2.start();
- latch.await(); // wait until both theads are executed
- System.out.println("Done");
- }
- public class Ex13 implements Runnable {
- int i=0;
- public void run() {
- System.out.println("Running "+ increment());
- }
- private synchronized int increment() {
- return ++i;
- }
- }
- Thread t = Thread.currentThread();
- String name = t.getName();
- System.out.println("name=" + name);
Add Comment
Please, Sign In to add comment