多线程编程
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package com.heibaiying.reentrantLock;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 利用ReentrantLock实现线程安全
|
||||
*/
|
||||
public class J1_ThreadSafe {
|
||||
|
||||
private static ReentrantLock reentrantLock = new ReentrantLock();
|
||||
private static Integer i = 0;
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int j = 0; j < 100000; j++) {
|
||||
try {
|
||||
reentrantLock.lock();
|
||||
i++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
reentrantLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread1 = new Thread(new IncreaseTask());
|
||||
Thread thread2 = new Thread(new IncreaseTask());
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.heibaiying.reentrantLock;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 可重入性
|
||||
*/
|
||||
public class J2_Reentrant {
|
||||
|
||||
private static ReentrantLock reentrantLock = new ReentrantLock();
|
||||
private static Integer i = 0;
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int j = 0; j < 100000; j++) {
|
||||
try {
|
||||
reentrantLock.lock();
|
||||
reentrantLock.lock();
|
||||
reentrantLock.lock();
|
||||
i++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
reentrantLock.unlock();
|
||||
reentrantLock.unlock();
|
||||
reentrantLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread1 = new Thread(new IncreaseTask());
|
||||
Thread thread2 = new Thread(new IncreaseTask());
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.heibaiying.reentrantLock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class J3_TimeLimitedLock {
|
||||
|
||||
private static ReentrantLock reentrantLock = new ReentrantLock();
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
String threadName = Thread.currentThread().getName();
|
||||
// 指定锁定时间
|
||||
if (reentrantLock.tryLock(5, TimeUnit.SECONDS)) {
|
||||
System.out.println(threadName + "被执行");
|
||||
Thread.sleep(6000);
|
||||
} else {
|
||||
System.out.println(threadName + "获得锁失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
reentrantLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Thread thread01 = new Thread(new IncreaseTask());
|
||||
thread01.setName("线程1");
|
||||
thread01.start();
|
||||
Thread thread02 = new Thread(new IncreaseTask());
|
||||
thread02.setName("线程2");
|
||||
thread02.start();
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.heibaiying.reentrantLock;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 公平锁
|
||||
*/
|
||||
public class J4_FairLock {
|
||||
|
||||
// 参数为true,代表使用公平锁
|
||||
private static ReentrantLock fairLock = new ReentrantLock(true);
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
fairLock.lock();
|
||||
System.out.println(Thread.currentThread().getName() + "获得锁");
|
||||
fairLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Thread thread1 = new Thread(new IncreaseTask());
|
||||
Thread thread2 = new Thread(new IncreaseTask());
|
||||
Thread thread3 = new Thread(new IncreaseTask());
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread3.start();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user