Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestSync {
- private int value;
- private List<Integer> list = new ArrayList<>();
- public synchronized int increment() {
- for (int i = 0; i < 1000; i++) {
- list.add(value++);
- }
- return list.size();
- }
- public int getValue() {
- return this.value;
- }
- }
- TestSync s1 = new TestSync();
- Callable<String> callable = () -> {
- // Perform some computation
- new Thread(s1::increment).start();
- new Thread(s1::increment).start();
- System.out.println("Result: " + s1.getValue());
- System.out.println("Result: " + s1.getValue());
- Thread.sleep(1000);
- return "Hello from Callable";
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement