编写程序,创建个 Counter 对象(程序17.6),使用Runnable 创建100个任务,在每个任务中调用Counter对象的incrementO方法100 次,同时输出每个任务的任务号和Counter对象的count成员值。修改上述程序,分别采用方法同步、块同步和Lock 锁的方式使程序运行结果
(方法同步)
package Chapter_17;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
class Counter01{
private int count=0;
public synchronized int getUserCount() {
return count;
}
public synchronized void increment() {
count++;
}
public synchronized void decrement() {
count--;
}
}
public class MethodCounter{
public static void main(String[] args) {
Counter01 counter01=new Counter01();
for (int j = 1; j < 101; j++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runnable runnable=()->{
for (int i = 1; i <101; i++) {
counter01.increment();
}
};
Executor executor=Executors.newCachedThreadPool();
executor.execute(runnable);
System.out.println("第"+j+"次任务 ,count值为:"+counter01.getUserCount());
}
}
}
(2)块同步
package Chapter_17;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
class Counter02{
private int count=0;
public int getUserCount() {
return count;
}
public void increment() {
count++;
}
public void decrement() {
count--;
}
}
public class ChunkCounter {
public static void main(String[] args) {
Counter02 counter02=new Counter02();
for (int j = 1; j < 101; j++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runnable runnable=()->{
for (int i = 1; i <101; i++) {
synchronized (counter02) {
counter02.increment();
}
}
};
Executor executor=Executors.newCachedThreadPool();
executor.execute(runnable);
System.out.println("第"+j+"次任务 ,count值为:"+counter02.getUserCount());
}
}
}
(3)Lock锁一
package Chapter_17;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Counter03{
private int count=0;
public int getUserCount() {
return count;
}
public void increment() {
count++;
}
public void decrement() {
count--;
}
}
public class LockCounter implements Runnable {
private final Lock banLock=new ReentrantLock();
Counter03 counter03=new Counter03();
public void operateAccount(){
banLock.lock();
try {for (int j = 1; j <= 100; j++) {
counter03.increment();
System.out.println("任务为:" + Thread.currentThread().getName() + (" count值" + counter03.getUserCount()));
}
} finally {
banLock.unlock();
}
}
public void run() {
operateAccount();
}
public static void main(String[] args){
LockCounter lockCounter=new LockCounter();
for (int i = 1; i <= 100; i++) {
Thread thread1=new Thread(lockCounter);
thread1.start();
}
}
}
(3)Lock锁二
package Chapter_17;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest {
public static void main(String[] args) {
MyService service = new MyService();
for (int j = 1; j <= 100; j++) {
MyThread a1 = new MyThread(service);
a1.start();
}
}
static public class MyService {
private int count=0;
public int getUserCount() {
return count;
}
public void increment() {
count++;
}
public void decrement() {
count--;
}
private Lock lock = new ReentrantLock();
public void testMethod() {
lock.lock();
try {
for (int i = 1; i <= 100; i++) {
increment();
System.out.println("任务为:" + Thread.currentThread().getName() + (" count值" + getUserCount()));
}
} finally {
lock.unlock();
}
}
}
static public class MyThread extends Thread {
private MyService service;
public MyThread(MyService service) {
super();
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
}