Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过同步块和方法在java中使用两个线程的程序中进行增量和减量_Java_Multithreading - Fatal编程技术网

通过同步块和方法在java中使用两个线程的程序中进行增量和减量

通过同步块和方法在java中使用两个线程的程序中进行增量和减量,java,multithreading,Java,Multithreading,我希望当dec调用时,它必须进行两次递减,然后它的递减值将传递给inc,然后递增1,这样,当递减值等于零时,它将停止程序。。 代码在这里 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;

我希望当dec调用时,它必须进行两次递减,然后它的递减值将传递给inc,然后递增1,这样,当递减值等于零时,它将停止程序。。 代码在这里

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 < array.length; z++) {
        array[z] = 0;
        System.out.print(" " + array[z]);
      }
      System.out.println();
      new B(dj);
      new A(dj);
    }

我看不出你的程序是如何满足你的需求的;每一个新的A都会创建一个新的B,这会创建另一个新的A。您的代码似乎会循环生成对象和线程!当传递的参数为非正时,是否省略了不会生成其他对象的保护

Enter your desired number:  4
0 0 0 0
Value of a in class B 4
Before Decrementing pre 4
Decrementing first 3
Value of second count: 2
p out2
Value of a 2
Value of count 2
Value of a in class B 2
Before Decrementing pre 2
Decrementing first 1
Value of second count: 0
p out0
Value of a 1
Value of count 1
Value of a in class B 1
Before Decrementing pre 1
Decrementing first 0
Value of second count: -1
p out-1
Value of a 0
Value of count 0
Value of a in class B 0
Count values cannot be negative
Value of a 0
Value of count 0
Value of a in class B 0
Count values cannot be negative

...

启动调用没有遵循.t-连接调用使我相信您的代码充其量是不确定性的。

这是一个非常麻烦的问题,请考虑发布SSCCE或类似的问题。我的程序没有错误,但没有提供所需的输出。这句话是假的不过,严肃地说,请清理代码,删除空行和注释掉的行,修复缩进。还请解释您希望看到的输出,以及实际输出是什么。