Skip to content

Commit

Permalink
线程不安全及其写法
Browse files Browse the repository at this point in the history
  • Loading branch information
developlee committed Jul 1, 2018
1 parent 06eaf76 commit d1bb6f6
Show file tree
Hide file tree
Showing 17 changed files with 623 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>

</dependencies>

<build>
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/com/developlee/ConcurrencyApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package com.developlee;

import com.developlee.filter.HttpFilter;
import com.developlee.interceptor.HttpInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class ConcurrencyApplication {
public class ConcurrencyApplication implements WebMvcConfigurer {

public static void main(String[] args) {
SpringApplication.run(ConcurrencyApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ConcurrencyApplication.class, args);
}

@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new HttpFilter());
registrationBean.addUrlPatterns("/threadLocal/**");

return registrationBean;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/threadLocal");
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/developlee/collections/ArrayListEg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.developlee.collections;

import com.developlee.annotations.ThreadUnsafe;
import com.developlee.unThreadSafe.ConcurrencyTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by Leson on 2018/7/1.
*/
@ThreadUnsafe
public class ArrayListEg {

private final static Logger logger = LoggerFactory.getLogger(ConcurrencyTest.class);

private static List<Integer> list = new ArrayList<>();

//请求总数
public static int clientTotal = 5000;

//同时并发执行的线程总数
public static int threadTotal = 200;

public static void main(String args[]) throws InterruptedException {
//新建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量(定义允许并发的数目)
final Semaphore semaphore = new Semaphore(threadTotal);
//
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
//请求放入线程池内
final int count = i;
executorService.execute(() ->{
try {
semaphore.acquire();//引入信号量
update(count);
semaphore.release(); //释放
} catch (InterruptedException e) {
logger.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭
logger.info("size {}",list.size());
}

private static void update(int i){
list.add(i);
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/developlee/collections/HashMapEg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.developlee.collections;

import com.developlee.annotations.ThreadUnsafe;
import com.developlee.unThreadSafe.ConcurrencyTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by Leson on 2018/7/1.
*/
@ThreadUnsafe
public class HashMapEg {
private final static Logger logger = LoggerFactory.getLogger(ConcurrencyTest.class);

private static Map<Integer, Integer> map = new HashMap<>();

//请求总数
public static int clientTotal = 5000;

//同时并发执行的线程总数
public static int threadTotal = 200;

public static void main(String args[]) throws InterruptedException {
//新建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量(定义允许并发的数目)
final Semaphore semaphore = new Semaphore(threadTotal);
//
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
//请求放入线程池内
final int count = i;
executorService.execute(() -> {
try {
semaphore.acquire();//引入信号量
update(count);
semaphore.release(); //释放
} catch (InterruptedException e) {
logger.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭
logger.info("size {}", map.size());
}

private static void update(int i) {
map.put(i, i);
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/developlee/collections/HashSetEg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.developlee.collections;

import com.developlee.annotations.ThreadUnsafe;
import com.developlee.unThreadSafe.ConcurrencyTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by Leson on 2018/7/1.
*/
@ThreadUnsafe
public class HashSetEg {

private final static Logger logger = LoggerFactory.getLogger(ConcurrencyTest.class);

private static Set<Integer> list = new HashSet<>();

//请求总数
public static int clientTotal = 5000;

//同时并发执行的线程总数
public static int threadTotal = 200;

public static void main(String args[]) throws InterruptedException {
//新建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量(定义允许并发的数目)
final Semaphore semaphore = new Semaphore(threadTotal);
//
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
//请求放入线程池内
final int count = i;
executorService.execute(() ->{
try {
semaphore.acquire();//引入信号量
update(count);
semaphore.release(); //释放
} catch (InterruptedException e) {
logger.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭
logger.info("size {}",list.size());
}

private static void update(int i){
list.add(i);
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/developlee/commonUnsafe/DateExample1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.developlee.commonUnsafe;

import com.developlee.annotations.ThreadUnsafe;
import com.developlee.unThreadSafe.ConcurrencyTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by Leson on 2018/7/1.
*/
@ThreadUnsafe
public class DateExample1 {

private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
private final static Logger logger = LoggerFactory.getLogger(ConcurrencyTest.class);

//请求总数
public static int clientTotal = 5000;

//同时并发执行的线程总数
public static int threadTotal = 200;

public static void main(String args[]) throws InterruptedException {
//新建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量(定义允许并发的数目)
final Semaphore semaphore = new Semaphore(threadTotal);
//
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
//请求放入线程池内
executorService.execute(() ->{
try {
semaphore.acquire();//引入信号量
update();
semaphore.release(); //释放
} catch (InterruptedException e) {
logger.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭

}

private static void update(){
try {
simpleDateFormat.parse("20180701");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/developlee/commonUnsafe/DateExample2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.developlee.commonUnsafe;

import com.developlee.annotations.ThreadSafe;
import com.developlee.unThreadSafe.ConcurrencyTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by Leson on 2018/7/1.
*/
@ThreadSafe //局部变量方式
public class DateExample2 {

private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
private final static Logger logger = LoggerFactory.getLogger(ConcurrencyTest.class);

//请求总数
public static int clientTotal = 5000;

//同时并发执行的线程总数
public static int threadTotal = 200;

public static void main(String args[]) throws InterruptedException {
//新建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//定义信号量(定义允许并发的数目)
final Semaphore semaphore = new Semaphore(threadTotal);
//
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
//请求放入线程池内
executorService.execute(() ->{
try {
semaphore.acquire();//引入信号量
update();
semaphore.release(); //释放
} catch (InterruptedException e) {
logger.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown(); //关闭

}

private static void update(){
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
simpleDateFormat.parse("20180701");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit d1bb6f6

Please sign in to comment.