整个例子看看啊~~~ 多谢! public class Main{ public static void main(String[] args) throws InterruptedException { Fair fair = new Fair(true);//true 用使用公平策略 false 使用非公平策略 for(int i=1; i< 50; i++){ new FairTestThread(fair, (new Random().nextInt(2) + 1)).start(); }//for } }public class Fair { private final ReentrantLock lock; public Fair(boolean fair){ lock = new ReentrantLock(fair); }
100 test1
200
500 test2
你试试改成5000。如果sleep比较短(500),那么结果:
100 test1
500 test2
200如果没有sleep,那么结果:
100 test1
500 test2
500 test2个人认为,要想测试多线程+同步,最好使用线程池,然后给每个线程加上线程名称在打印的时候打印出来。
start()方法只是代表一个线程进入了就绪状态,并不代表它就会执行。所以你每次执行的顺序都不一定一样。第二个问题:
问题太含糊。第三个问题:
你这里
public synchronized void test1()
的写法基本等于 synchronized(this),也就是调用test1()或test2()方法时等于给当前TestSync对象加了同步锁,所以每次都只有一个线程根据判断能调用到test1()或test2()
当t10, t11调用的是test1()方法,t10,t11是同步的;
t20调用test2()方法,在t10,t11在未改变test.data的值之前,已经将test.data修改了
test1(), test2()同样使用synchronized关键字,锁定的是TestSync对象;我调用的是顺序是t10,t11,t20, 但是实际运行下来则是t10,t20,t11, 这样让我很费解。
synchronized是怎样将锁传给下一个执行单元呢?如果main函数,没有sleep()方法,首先执行的是test2()方法。
我调用的顺序是t10,t11,t20, 同时test1(),test2()都使用sync关键字,但是执行的顺序是不同的?为什么呢?
我在main()函数中sleep()方法是故意加上去的,请看上面的回帖。
if(this.flag==false)
{
test.data= 100;
Thread.sleep(2000);
System.out.println(test.data+" test1");
this.flag= true;
}
问题一:t10最先运行,500毫秒后t11开始运行并等待test1()的锁释放,注意是2000毫秒后test1()才释放,在t11等待的过程中t20开始运行并且也等待t10 释放test1()的锁。所以t11,t20就会进入等待队列ContentionList,但由于ContentionList是后进先出,所以t20先运行。
高手,针对这个问题,我很感兴趣,我专门开贴进行提问,有劳您大驾,给解答一下
http://bbs.csdn.net/topics/390458387
上网搜了一下,似乎确实是这样,太让我惊讶了,我一直以为线程对锁的竞争是公平的,真无语。
Java的锁分内置锁(synchronized)和显式锁(java.util.locks.ReentrantLock),内置锁使用的是非公平策略,而显式锁默认使用的是非公平策略,当然也可以在创建显式锁对象时设置其为公平策略。但公平锁会影响
能否推荐一下关于这类问题的资料。
多谢!
建议多看一下深入java虚拟机之类的书对你会有帮助。
整个例子看看啊~~~
多谢!
public class Main{
public static void main(String[] args) throws InterruptedException {
Fair fair = new Fair(true);//true 用使用公平策略 false 使用非公平策略
for(int i=1; i< 50; i++){
new FairTestThread(fair, (new Random().nextInt(2) + 1)).start();
}//for
}
}public class Fair {
private final ReentrantLock lock; public Fair(boolean fair){
lock = new ReentrantLock(fair);
}
//模拟一个较耗时的task
public void testFair1() throws InterruptedException{
lock.lock();
try{
for(int i=0; i<100000; i++){}
}finally{
lock.unlock();
}//finally
} //模拟一个较快完成的task
public void testFair2() throws InterruptedException{
lock.lock();
try{
for(int i=0; i<1000; i++){}
}finally{
lock.unlock();
}//finally
}
}public class FairTestThread extends Thread {
private Fair fair;
private int type = 1;
public FairTestThread(Fair fair, int type) {
this.fair = fair;
this.type = type;
} public void run() {
long start;
try {
while (!Thread.currentThread().isInterrupted()) {
start = System.nanoTime();
if(type == 1)
fair.testFair1();
else
fair.testFair2();
System.out.println((System.nanoTime() - start));
}// while
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}//catch
}
}