0

i have an Assignment about Multithreading and i need some help.

I have a Ressource class which cannot be changed

public class Ressource {
    public int val;
    public void incr() {
        val++;

    }
    public void decr() {
        val--;

    }

And i have my main Class

public class TwoThreads {
    public static Ressource res = new Ressource();
    public static void main(String[] args)  {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    res.incr();
                }
                System.out.println(res.val);
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    res.decr();
                }
                System.out.println(res.val);
            }
        });
        t1.start();
        t2.start();

    }
}

I tried to use synchronized inside my Override methods but it didn't work. I know that if i used

 public synchronized void incr() {
        val++;

    }

it will work but i should not change anything in the Ressources Class. Any Ideas?

  • You could use `synchronized (res) {}` perhaps – user Jun 02 '20 at 14:56
  • 1
    What is the purpose of the exercise? – NormR Jun 02 '20 at 14:57
  • You want to count till 100 and then count down to 0? – Majid Roustaei Jun 02 '20 at 14:58
  • i want to increase to 100 and then decrease to 0.First t1 should run and when its finished should t2 start.But i have to do it in the main method. –  Jun 02 '20 at 14:59
  • If you want to wait for a thread, use `t1.join()` – user Jun 02 '20 at 15:04
  • I don't get how it would work if you have `synchronized void incr()` even if you put synchronized on both incr, and decr, you could an increment and then a decrement before all of the increments are done. – matt Jun 02 '20 at 15:19

1 Answers1

3

i want to increase to 100 and then decrease to 0.First t1 should run and when its finished should t2 start.But i have to do it in the main method.

You can do it as follows:

t1.start();
t1.join();
t2.start();

Demo:

class Ressource {
    public int val;

    public void incr() {
        val++;

    }

    public void decr() {
        val--;

    }
}

public class Main {
    public static Ressource res = new Ressource();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(res.val);
                    res.incr();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(res.val);
                    res.decr();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(res.val);
            }
        });
        t1.start();
        t1.join();
        t2.start();
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110