java多线程安全加锁
2017-08-28 本文已影响50人
张不二01
1,同步锁
2,lock锁(更加灵活)
3,锁对象的等待和唤醒
1,同步锁
public class IMTicketSeller implements Runnable {
private int ticketCount = 1000;
private Object obj = new Object();
@Override
public void run() {
while (true){
/*synchronized (obj) {
if (ticketCount > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + ticketCount--);
}
}*/
sellTicket();
}
}
//这里同步用synchronized,不用创建对象作为参数传给同步锁
public synchronized void sellTicket(){
if (ticketCount > 0){
System.out.println(Thread.currentThread().getName() + " " + ticketCount--);
}
}
}
//测试:
public class Test {
public static void main(String[] args) {
//开两个窗口进行票务售卖
ExecutorService exeService = Executors.newFixedThreadPool(3);
IMTicketSeller seller = new IMTicketSeller();
exeService.submit(seller);
exeService.submit(seller);
exeService.shutdown();
}
}
2,lock锁(更加灵活),也就是把1中的同步锁换成lock锁
/*
* 使用系统自带的lock,取代掉同步代码块
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class IMTicketSeller01 implements Runnable {
private int ticketCount = 1000;
private Lock lock = new ReentrantLock();
@Override
public void run() {
while (true){
sellTicket();
}
}
//这里同步用synchronized,不用创建对象作为参数传给同步锁
public void sellTicket(){
lock.lock();
if (ticketCount > 0){
System.out.println(Thread.currentThread().getName() + " " + ticketCount--);
}
lock.unlock();
}
}
//测试:
public class Test01 {
public static void main(String[] args) {
//开两个窗口进行票务售卖
AtomicReference<ExecutorService> exeService = new AtomicReference<>(Executors.newFixedThreadPool(3));
IMTicketSeller01 seller = new IMTicketSeller01();
exeService.get().submit(seller);
exeService.get().submit(seller);
exeService.get().shutdown();
}
}
3,锁对象的等待和唤醒
*情景是:一个Person类,一个输入对象负责给Person赋值,一个输出对象负责打印Person,输出对象需要等待输入对象完成之后才能进行输出。这里输入对象是做的一个循环,奇数和偶数输入两个不同的内容 *
//Person类
public class public class Person {
String name;
String sex;
boolean isAssigned = false;
//把构造方法私有化
Person(){
super();
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
} {
String name;
String sex;
boolean isAssigned = false;
//把构造方法私有化
Person(){
super();
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
//Input输入对象类
public class public class Input implements Runnable{
private Person p;
public Input(Person p) {
this.p = p;
}
@Override
public void run() {
System.out.println("00000");
int i = 0;
while (true) {
synchronized (p){
//锁对象的唤醒和等待必须要在同步锁内部
if (p.isAssigned == true) {
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i % 2 == 0) {
p.name = "zhangdanfeng";
p.sex = "man";
}else{
p.name = "0000000";
p.sex = "11111111`";
}
i++;
p.isAssigned = true;
p.notify();
}
}
}
} implements Runnable{
private Person p;
public Input(Person p) {
this.p = p;
}
@Override
public void run() {
System.out.println("00000");
int i = 0;
while (true) {
synchronized (p){
//锁对象的唤醒和等待必须要在同步锁内部
if (p.isAssigned == true) {
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i % 2 == 0) {
p.name = "zhangdanfeng";
p.sex = "man";
}else{
p.name = "0000000";
p.sex = "11111111`";
}
i++;
p.isAssigned = true;
p.notify();
}
}
}
}
//Output输出类
public class Output implements Runnable {
private Person p;
public Output(Person p) {
this.p = p;
}
@Override
public void run() {
System.out.println("11111");
while (true) {
synchronized (p) {
//锁对象的唤醒和等待必须要在同步锁内部
if (p.isAssigned == false) {
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(p);
p.isAssigned = false;
p.notify();
}
}
}
}
//测试
public class Test {
public static void main(String[] args) {
Person person = new Person();
Input input = new Input(person);
Output output = new Output(person);
new Thread(input).start();
new Thread(output).start();
}
}