先看个代码吧!
synchronized的执行方式是:谁获得了锁,谁就执行,其他的线程都会处于等待状态,等当前线程执行完之后,根据系统的线程调用规则来调用数据.可以这样说吧,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修饰的代码块差不多!
大家把例子运行下就能看出来差别,如有不对的地方欢迎大家拍砖.