老谈synchronized,lock的区别及问题.

先看个代码吧!

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestThread {
public static long t=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根

TestThread2 tt = new TestThread2();
System.out.println(t=System.currentTimeMillis());
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();

}
}

class TxtThread implements Runnable {
int num = 100;
String str = new String();

public void run() {
while (num > 0) {
synchronized (str) {
if (num > 0) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.getMessage();
}
System.out.println(Thread.currentThread().getName()
+ "this is " + num--);
}
}
}
}
}

class TestThread2 implements Runnable {
int num = 100;
String str = new String();

@Override
public void run() {
Lock lock=new ReentrantLock();
while (num > 0) {
lock.lock();
try {
boolean b=lock.tryLock(100,TimeUnit.MILLISECONDS);
if (num > 0) {
Thread.sleep(100);
}
if(num==0){
return;
}
System.out.println(Thread.currentThread().getName()
+ "this is " + num--);
} catch (Exception e) {
e.getMessage();
}finally{
lock.unlock();
}
}
System.out.println("cost time is: "+(System.currentTimeMillis()-TestThread.t));
}

}


synchronized的执行方式是:谁获得了锁,谁就执行,其他的线程都会处于等待状态,等当前线程执行完之后,根据系统的线程调用规则来调用数据.可以这样说吧,synchronized比较死板,不灵活,所以就要使用Lock锁来进行同步了! 上面的例子不是太清晰,再发个

package com.virusyang.tools;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest2 {

/**
* @param args
*/
private static long c = 0;

public static void main(String[] args) {
final Lock l = new ReentrantLock();
Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 10;) {
try {
if (l.tryLock()) {
c++;
i++;

System.out.println(Thread.currentThread().getName()
+ " t1 this is " +c);
l.unlock();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 10;) {

try {
if (l.tryLock(250,TimeUnit.MILLISECONDS)) {
c++;
i++;
System.out.println(Thread.currentThread().getName()
+ " t2 this is " +c);
l.unlock();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
};
long t = System.currentTimeMillis();
Thread t3 = new Thread() {
public void run() {
for (int i = 0; i < 10;) {
try {
if (l.tryLock()) {
c++;
i++;

System.out.println(Thread.currentThread().getName()
+ " t3 this is " +c);
l.unlock();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};

Thread t4 = new Thread() {
public void run() {
for (int i = 0; i < 10;) {
try {
if (l.tryLock()) {
c++;
i++;

System.out.println(Thread.currentThread().getName()
+ " t4 this is " +c);
l.unlock();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};


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

}

}
这个例子执行的时候,分为lock(),trylock(),trylock(arg,arg)三种方法,其中使用第三种方法的时候线程的执行是独占的,trylock()是在系统空闲时就申请,不会等待,没有第三种方法灵活,lock()则和synchronized修饰的代码块差不多!

大家把例子运行下就能看出来差别,如有不对的地方欢迎大家拍砖.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值