Note that there are some explanatory texts on larger screens.

plurals
  1. POincrement and decrement in program using two threads in java through synchronized blocks and methods
    primarykey
    data
    text
    <p>I want that when the dec() calls it must make two decrements and then its decremented value will pass to inc() and then increment by 1 so that a time come when the decremented value equal to zero and it stops the program.. Code is here</p> <pre><code>package thread_array; import java.io.*; import java.lang.Thread; import java.util.Scanner; class A extends Thread { public static int count; public static int a, b; Thread t; public static int i; A(int i) { synchronized (this) { a = i; System.out.println("Value of a " + a); count = a; System.out.println("Value of count " + count); t = new Thread(this); t.start(); new B(count); } } @Override public void run() { inc(); } public synchronized void inc() { try { if (count != 0) { synchronized (this) { System.out.println("Before Incrementing pre " + count); ++count; System.out.println("Incrementing pre " + count); System.out.println("Incrementing in value of p " + count); Thread.sleep(2000); } } else { System.out.println("Count values cannot be negative"); } } catch (InterruptedException e) { // ignore this } } } class B extends Thread { public static int count; public static int a, b; Thread t; public static int i; B(int i) { a = i; System.out.println("Value of a in class B " + a); count = a; t = new Thread(this); t.start(); new A(count); } @Override public void run() { dec(); } public synchronized void dec() { try { if (count != 0) { synchronized (this) { System.out.println("Before Decrementing pre " + count); b = count--; System.out.println("Decrementing first " + count); count--; System.out.println("Value of second count: " + count); Thread.sleep(1000); System.out.println("p out" + count); } } else { System.out.println("Count values cannot be negative"); } } catch (InterruptedException e) { // ignore exception } } } class Thread_array extends Thread implements Runnable { public static void main(String[] args) throws IOException, InterruptedException { int z; System.out.print("Enter your desired number: "); Scanner input = new Scanner(System.in); int dj = input.nextInt(); int[] array = new int[dj]; for (z = 0; z &lt; array.length; z++) { array[z] = 0; System.out.print(" " + array[z]); } System.out.println(); new B(dj); new A(dj); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload