Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package watki;
- class Thread03 implements Runnable {
- int liczba = 2;
- Object o = new Object();
- public void run( ) {
- while (true) {
- synchronized (o) {
- liczba++;
- liczba--;
- }
- if (Thread.currentThread().isInterrupted())
- return;
- }
- }
- public int getLiczba() {
- synchronized (o) {
- return liczba;
- }
- }
- }
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- Thread03 r = new Thread03();
- Thread t = new Thread(r);
- t.start();
- for (int i = 0; i < 1000000; i++) {
- int l = r.getLiczba();
- if (l % 2 != 0)
- System.out.println(l);
- }
- t.interrupt();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement