因为ConcurrentLinkedQueue是线程序安全的并且是针对并发的
主类
- public class conn{
- public static void main(String[] args) throws Exception{
- Queue<String> queue=new ConcurrentLinkedQueue<String>();
- for ( int i= 0 ;i< 1000000 ;i++){
- queue.add(String.valueOf(i));
- }
- int num= 10 ; //线程人个数
- for ( int i= 0 ;i<num;i++){
- new ThreadConn(queue);
- }
- }
- }
线程类
- public class ThreadConn implements Runnable{
- Queue<String> queue;
- public ThreadConn(Queue<String> queue){
- this .queue=queue;
- Thread thread=new Thread( this );
- thread.start();
- }
- public void run(){
- try {
- long sd= new Date().getTime();
- while (queue.poll()!= null ){
- //这里是业务逻辑
- }
- System.out.println (sn-sd);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }