Synchronized import org.junit.Test;import java.util.concurrent.CountDownLatch;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * Created by 克瑞普斯 on 2017-09-14. */public class App{ private Ob
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by 克瑞普斯 on 2017-09-14.
*/
public class App
{
private Object _Lock = new Object();
@Test
public void sys() throws InterruptedException
{
long start = System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++)
{
int finalI = i;
new Thread(() -> {
synchronized (_Lock)
{
System.out.println(">>>>> " + finalI);
synchronized (_Lock)
{
bi(latch, finalI);
}
}
}).start();
}
latch.await();
long totalTime = System.currentTimeMillis() - start;
System.out.println("totalTime : " + totalTime);
}
private Lock lock = new ReentrantLock();
@Test
public void loc() throws InterruptedException
{
long start = System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++)
{
int finalI = i;
new Thread(() -> {
lock.lock();
try
{
System.out.println(">>>>> " + finalI);
lock.lock();
try
{
bi(latch, finalI);
} finally
{
lock.unlock();
}
}finally
{
lock.unlock();
}
}).start();
}
latch.await();
long totalTime = System.currentTimeMillis() - start;
System.out.println("totalTime : " + totalTime);
}
private void bi(CountDownLatch latch, int finalI)
{
try
{
Thread.sleep(1000);
System.out.println("!!!!! " + finalI + "\n");
latch.countDown();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
