更新 thread
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 可重入锁测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-10:27
|
||||
*/
|
||||
public class ReentrantLockTest {
|
||||
|
||||
private Lock lock = new ReentrantLock();
|
||||
private Condition conditionA = lock.newCondition();
|
||||
private Condition conditionB = lock.newCondition();
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ReentrantLockTest test = new ReentrantLockTest();
|
||||
new Thread(() -> test.testLock()).start();
|
||||
new Thread(() -> test.testLock()).start();
|
||||
|
||||
Thread t = new Thread(() -> test.awaitA());
|
||||
t.start();
|
||||
Thread.sleep(2000);
|
||||
test.signalA();
|
||||
|
||||
}
|
||||
|
||||
public void awaitA() {
|
||||
lock.lock();
|
||||
try {
|
||||
System.out.println("before awaitA at " + System.currentTimeMillis());
|
||||
conditionA.await(); // 在此之前必须获得锁,不然报错illegalMonitorStateException 错误
|
||||
System.out.println("after awaitA at " + System.currentTimeMillis());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
System.out.println(" 释放锁 awaitA ");
|
||||
}
|
||||
}
|
||||
|
||||
public void signalA() {
|
||||
lock.lock();
|
||||
try {
|
||||
System.out.println("signalA at " + System.currentTimeMillis());
|
||||
conditionA.signal(); // 在此之前必须获得锁,不然报错illegalMonitorStateException 错误
|
||||
System.out.println("signalA over at " + System.currentTimeMillis());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
System.out.println(" 释放锁 signalA ");
|
||||
}
|
||||
}
|
||||
|
||||
public void testLock() {
|
||||
lock.lock();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println();
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* 读写锁测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-11:48
|
||||
*/
|
||||
public class ReentrantReadWriteLockTest {
|
||||
|
||||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
public void read(){
|
||||
lock.readLock().lock(); // 读锁
|
||||
System.out.println("获得读锁 " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void write(){
|
||||
lock.writeLock().lock(); // 写锁
|
||||
System.out.println("获得写锁 " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1. 读读共享, A 和 B 同时获得锁
|
||||
ReentrantReadWriteLockTest test = new ReentrantReadWriteLockTest();
|
||||
Thread t1 = new Thread(()-> {
|
||||
Thread.currentThread().setName("A");
|
||||
test.read();
|
||||
});
|
||||
Thread t2 = new Thread(()-> {
|
||||
Thread.currentThread().setName("B");
|
||||
test.read();
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
// 2. 写写互斥, D线程比C线程落后两秒执行
|
||||
t1 = new Thread(()->{
|
||||
Thread.currentThread().setName("C");
|
||||
test.write();
|
||||
});
|
||||
t2 = new Thread(()->{
|
||||
Thread.currentThread().setName("D");
|
||||
test.write();
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
36
code/src/main/java/com/raorao/java/thread/TimerTest.java
Normal file
36
code/src/main/java/com/raorao/java/thread/TimerTest.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* 定时器.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-13:07
|
||||
*/
|
||||
public class TimerTest {
|
||||
private static Timer timer = new Timer();
|
||||
|
||||
static class MyTask extends TimerTask{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("运行时间: " + new Date());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyTask task1 = new MyTask();
|
||||
try {
|
||||
Date taskDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-08-06 13:15:00");
|
||||
System.out.println("执行时间: " + taskDate.toLocaleString() + ",当前时间" + new Date().toLocaleString());
|
||||
timer.schedule(task1, taskDate);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user