解决线程安全的另一种思路

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmp;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vicky.H
 * @email eclipser@163.com
 */
public class LockTest2 {

    public static void main(String[] args) {
        Thread t1 = new Thread(new T("t1"));
        Thread t2 = new Thread(new T("t2"));
        Thread t3 = new Thread(new T("t3"));
        Thread t4 = new Thread(new T("t4"));
        Thread t5 = new Thread(new T("t5"));

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}

class T implements Runnable {

    private String name;

    public T(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        boolean isReady = false;
        synchronized (LockTest2Manager.syncObj) {
            isReady = LockTest2Manager.ready;
            LockTest2Manager.ready = false;
        }
        try {

            if (isReady) { // 当然自己这样做正常情况下是不允许的,但某些情况下也可以使用,例如:游戏中,客户端发起的请求。就可以通过返回繁忙,让客户端玩家自己再发起请求
                System.out.println("执行" + name + " begin");
                for (int i = 0; i < 10; i++) {
                    System.out.println(name + LockTest2Manager.getInstance().getList().get(i));
                }
                System.out.println("执行" + name + " end");
            } else {
                System.out.println("执行" + name + " fail 繁忙中!");
            }


        } finally {
            synchronized (LockTest2Manager.syncObj) {
                LockTest2Manager.ready = true;
            }
        }

    }
}



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmp;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vicky.H
 * @email eclipser@163.com
 */
public class LockTest2Manager {

    public static boolean ready = true;
    public static final Object syncObj = new Object();
    
    private List<U> list;

    private LockTest2Manager() {
        list = new ArrayList<U>();
        for (int i = 0; i < 10; i++) {
            list.add(new U(i, "name" + i));
        }
    }
    
    private static LockTest2Manager instance;

    public static LockTest2Manager getInstance() {
        if (instance == null) {
            instance = new LockTest2Manager();
        }
        return instance;
    }

    public List<U> getList() {
        return list;
    }

    public void setList(List<U> list) {
        this.list = list;
    }
    
}

class U {

    private int id;
    private String name;

    public U(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "U{" + "id=" + id + ", name=" + name + '}';
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值