0% found this document useful (0 votes)
11 views1 page

# AtomicIntegerExample

The document presents a Java program that demonstrates the use of AtomicInteger for thread-safe incrementing and decrementing operations. It includes a Producer thread that increments the AtomicInteger and a Consumer thread that decrements it. The final result is printed after both threads complete their execution.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

# AtomicIntegerExample

The document presents a Java program that demonstrates the use of AtomicInteger for thread-safe incrementing and decrementing operations. It includes a Producer thread that increments the AtomicInteger and a Consumer thread that decrements it. The final result is printed after both threads complete their execution.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# AtomicIntegerExample

import [Link].*;

public class AtomicIntegerExample {

public static void main(String[] args) {


AtomicInteger atmint = new AtomicInteger(0);

Thread producer = new Thread(new Producer(atmint));


Thread consumer = new Thread(new Consumer(atmint));

[Link]();
[Link]();
try {
[Link]();
[Link]();
} catch (InterruptedException s) {}
[Link]("result: atmint="+[Link]());
}
}

class Producer implements Runnable{

private AtomicInteger atm;

public Producer(AtomicInteger atmval) {


[Link] = atmval;
}

public void run() {

for (int i = 0; i < 10000002; i++) {


[Link](); // atm++;
}
}
}

class Consumer implements Runnable{

private AtomicInteger atm;

public Consumer(AtomicInteger atmval) {


[Link] = atmval;
}

public void run() {


for (int i = 0; i < 10000000; i++) {
[Link](); // atm--;
}
}
}

You might also like