如何让 AtomicInteger 在 Runnable 中工作 class
How to get AtomicInteger to work in a Runnable class
我在Java中将这段代码写成Runnable
,但我的教授要我添加AtomicInteger
,这样就没有线程干扰了。我该怎么做呢?我已经尝试查找如何在代码中使用它的示例,但我不知道在这种情况下该怎么做。
public class GarageWorker {
public static void main(String[] args) {
System.out.println("We are going to count the vehicles in the garage");
System.out.println();
System.out.println("There are 50 vehicles in the garage to start with!");
System.out.println();
GarageWorker garage = new GarageWorker(50);
Runnable vehicleEnter = garage.new Enter();
Runnable vehicleExit = garage.new Exit();
Thread thread1 = new Thread(vehicleEnter);
Thread thread2 = new Thread(vehicleExit);
thread1.start();
thread2.start();
}
public GarageWorker(int initialCarCount) {
counter = initialCarCount;
}
public int counter;
public class Enter implements Runnable {
public Enter() {
}
public int increaseVehicleCount() {
return ++counter;
}
public void run() {
while (counter < 100) {
System.out.println("One vehicle entered the garage now there are " + increaseVehicleCount());
}
}
}
public class Exit implements Runnable {
public Exit() {
}
public int decreaseVehicleCount() {
return --counter;
}
public void run() {
while (counter > 0) {
System.out.println("One vehicle left the garage now there are " + decreaseVehicleCount());
}
}
}
}
代码运行良好,但我的教授要我在代码中实现 AtomicInteger
class。
我假设您的教授希望您将 counter
更改为 AtomicInteger
。首先,您要导入 AtomicInteger
并更改 counter
的声明和构造函数以反映这一点。
import java.util.concurrent.atomic.AtomicInteger;
public GarageWorker(int initialCarCount) {
counter = new AtomicInteger(initialCarCount);
}
public AtomicInteger counter;
此外,您想更改 increaseVehicleCount
和 decreaseVehicleCount
方法以使用 AtomicInteger
而不是 int
。
public int increaseVehicleCount() {
return counter.incrementAndGet();
}
public int decreaseVehicleCount() {
return counter.decrementAndGet();
}
incrementAndGet
as well as decrementAndGet
do just as they say: they increment (or decrement) and subsequently returns the new value. For more information, take a look at the Javadoc.
干杯!
我在Java中将这段代码写成Runnable
,但我的教授要我添加AtomicInteger
,这样就没有线程干扰了。我该怎么做呢?我已经尝试查找如何在代码中使用它的示例,但我不知道在这种情况下该怎么做。
public class GarageWorker {
public static void main(String[] args) {
System.out.println("We are going to count the vehicles in the garage");
System.out.println();
System.out.println("There are 50 vehicles in the garage to start with!");
System.out.println();
GarageWorker garage = new GarageWorker(50);
Runnable vehicleEnter = garage.new Enter();
Runnable vehicleExit = garage.new Exit();
Thread thread1 = new Thread(vehicleEnter);
Thread thread2 = new Thread(vehicleExit);
thread1.start();
thread2.start();
}
public GarageWorker(int initialCarCount) {
counter = initialCarCount;
}
public int counter;
public class Enter implements Runnable {
public Enter() {
}
public int increaseVehicleCount() {
return ++counter;
}
public void run() {
while (counter < 100) {
System.out.println("One vehicle entered the garage now there are " + increaseVehicleCount());
}
}
}
public class Exit implements Runnable {
public Exit() {
}
public int decreaseVehicleCount() {
return --counter;
}
public void run() {
while (counter > 0) {
System.out.println("One vehicle left the garage now there are " + decreaseVehicleCount());
}
}
}
}
代码运行良好,但我的教授要我在代码中实现 AtomicInteger
class。
我假设您的教授希望您将 counter
更改为 AtomicInteger
。首先,您要导入 AtomicInteger
并更改 counter
的声明和构造函数以反映这一点。
import java.util.concurrent.atomic.AtomicInteger;
public GarageWorker(int initialCarCount) {
counter = new AtomicInteger(initialCarCount);
}
public AtomicInteger counter;
此外,您想更改 increaseVehicleCount
和 decreaseVehicleCount
方法以使用 AtomicInteger
而不是 int
。
public int increaseVehicleCount() {
return counter.incrementAndGet();
}
public int decreaseVehicleCount() {
return counter.decrementAndGet();
}
incrementAndGet
as well as decrementAndGet
do just as they say: they increment (or decrement) and subsequently returns the new value. For more information, take a look at the Javadoc.
干杯!