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