-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
developlee
committed
Jul 1, 2018
1 parent
06eaf76
commit d1bb6f6
Showing
17 changed files
with
623 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
src/main/java/com/developlee/commonUnsafe/DateExample1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
src/main/java/com/developlee/commonUnsafe/DateExample2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.