使用Runnable 创建100个任务,在每个任务中调用Counter对象的incrementO方法100 次,分别采用方法同步、块同步和Lock 锁

编写程序,创建个 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) {

// TODO Auto-generated catch block

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();

    }

    }

}




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值