public class ShareData {
private static ShareData shareData = new ShareData();
// 不安全的线程共享变量
private int x = 0;
private ShareData() {
}
public static ShareData getInstantce() {
return shareData;
}
public synchronized void addX() {
System.out.println(Thread.currentThread().getName() + "in");
x++;
System.out.println("x:" + x);
System.out.println(Thread.currentThread().getName() + "out");
}
public synchronized void subX() {
System.out.println(Thread.currentThread().getName() + "in");
x--;
System.out.println("x:" + x);
System.out.println(Thread.currentThread().getName() + "in");
}
}
模拟请求线程类:
public class OneRequestThread extends Thread {
private static final ShareData shareData = ShareData.getInstantce();
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String id;
private CountDownLatch latch;
public OneRequestThread(String id, CountDownLatch latch) {
super();
this.id = id;
this.latch = latch;
}
@Override
public void run() {
try {
System.out.println(id + "waitting");
latch.await(); // 一直阻塞当前线程,直到计时器的值为0
} catch (InterruptedException e) {
e.printStackTrace();
}
doPost();// 发送post 请求
}
private void doPost() {
System.out.println("模拟用户: " + id + " 模拟请求开始 at " + sdf.format(new Date()));
shareData.addX();
System.out.println("模拟用户: " + id + " 模拟请求结束 at " + sdf.format(new Date()));
}
}
主流程:
public class ThreadTest {
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(1);
for (int i = 0; i < 10; i++) {
OneRequestThread threadLog = new OneRequestThread("thread" + i, latch);
threadLog.start();
}
// 计数器減一 所有线程释放 并发访问。
latch.countDown();
System.out.println("所有模拟请求结束 at " + sdf.format(new Date()));
}
}
结果
thread0waitting
thread1waitting
thread2waitting
thread3waitting
thread4waitting
thread5waitting
thread6waitting
thread7waitting
thread8waitting
thread9waitting
模拟用户: thread9 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread5 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread8 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread1 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread3 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread4 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread0 模拟请求开始 at 2018-09-28 23:00:30
Thread-9in
模拟用户: thread6 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread7 模拟请求开始 at 2018-09-28 23:00:30
模拟用户: thread2 模拟请求开始 at 2018-09-28 23:00:30
所有模拟请求结束 at 2018-09-28 23:00:30
x:1
Thread-9out
Thread-2in
x:2
Thread-2out
模拟用户: thread9 模拟请求结束 at 2018-09-28 23:00:30
模拟用户: thread2 模拟请求结束 at 2018-09-28 23:00:30
Thread-7in
x:3
Thread-7out
Thread-6in
x:4
Thread-6out
模拟用户: thread7 模拟请求结束 at 2018-09-28 23:00:30
模拟用户: thread6 模拟请求结束 at 2018-09-28 23:00:30
Thread-0in
x:5
Thread-0out
Thread-4in
x:6
Thread-4out
模拟用户: thread0 模拟请求结束 at 2018-09-28 23:00:30
模拟用户: thread4 模拟请求结束 at 2018-09-28 23:00:30
Thread-3in
x:7
Thread-3out
Thread-1in
x:8
Thread-1out
模拟用户: thread3 模拟请求结束 at 2018-09-28 23:00:30
模拟用户: thread1 模拟请求结束 at 2018-09-28 23:00:30
Thread-8in
x:9
Thread-8out
Thread-5in
x:10
Thread-5out
模拟用户: thread8 模拟请求结束 at 2018-09-28 23:00:30
模拟用户: thread5 模拟请求结束 at 2018-09-28 23:00:30