...
Code Block | ||
---|---|---|
| ||
public class Container implements Runnable { private final Vector<Integer> vector = new Vector<Integer>(1000); public Vector<Integer> getVector() { return vector; } public synchronized void run() { Random number = new Random(123L); int i = 10vector.capacity(); while (i > 0) { vector.add(number.nextInt(100)); i--; } } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new Container()); thread.start(); Thread.sleep(5000); thread.stop(); } } |
...
Code Block | ||
---|---|---|
| ||
public class Container implements Runnable { private final Vector<Integer> vector = new Vector<Integer>(1000); private volatile boolean done = false; public Vector<Integer> getVector() { return vector; } public void shutdown() { done = true; } public synchronized void run() { Random number = new Random(123L); int i = 10vector.capacity(); while (!done && i > 0) { vector.add(number.nextInt(100)); i--; } } public static void main(String[] args) throws InterruptedException { Container container = new Container(); Thread thread = new Thread(container); thread.start(); Thread.sleep(5000); container.shutdown(); } } |
...
Code Block | ||
---|---|---|
| ||
public class Container implements Runnable { private final Vector<Integer> vector = new Vector<Integer>(1000); public Vector<Integer> getVector() { return vector; } public synchronized void run() { Random number = new Random(123L); int i = 10vector.capacity(); while (!Thread.interrupted() && i > 0) { vector.add(number.nextInt(100)); i--; } } public static void main(String[] args) throws InterruptedException { Container c = new Container(); Thread thread = new Thread(c); thread.start(); Thread.sleep(5000); thread.interrupt(); } } |
...