码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程实现

时间:2016-08-16 09:18:40      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

CrawlerMain:

package niuke;

public class CrawlerMain {

    public static void main(String[] args) {
        
        int threadnum = 2;        
    
        String mysqlIp = "localhost";
        String mysqlUsername = "root";
        String mysqlPassword = "root";
        
        //开启线程
        CrawlerThreadPool ssct = new CrawlerThreadPool(threadnum, mysqlIp, mysqlUsername, mysqlPassword);
        Thread ssctThread = new Thread(ssct);
        ssctThread.start();
    }
}

CrawlerThreadPool:

package niuke;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class CrawlerThreadPool implements Runnable, Observer {
    private ExecutorService pool;
    private List<Callable<Object>> commandList;

    public CrawlerThreadPool(int threadnum, String mysqlIp, String username, String password) {
        this.pool = Executors.newFixedThreadPool(threadnum);
        this.commandList = new LinkedList<Callable<Object>>();
        update();

    }

    private void ReleaseThreadPool() {
        pool.shutdown();
        while (!pool.isTerminated())
            try {
                synchronized (commandList) {
                    pool.awaitTermination(10, TimeUnit.SECONDS);
                }
            } catch (InterruptedException e) {
                System.err.println(e);
            }
    }

    public void run() {
        boolean cont = true;
        List<Callable<Object>> copyCommandList = new LinkedList<Callable<Object>>();

        //while (cont) {
            copyCommandList.clear();
            System.out.println("Crawler new turn begin:");
            synchronized (commandList) {
                while (commandList.size() == 0) {
                    try {
                        System.out.println("locking");
                        commandList.wait();
                    } catch (InterruptedException e) {
                        System.err.println(e);
                    }
                }
                System.out.println("add to list");
                copyCommandList.addAll(commandList);
            }
            System.out.println("start invokeall");
            try {
                pool.invokeAll(copyCommandList);
            } catch (InterruptedException e) {
                System.err.println(e);
                ReleaseThreadPool();
                cont = false;
            }
            System.out.println("Crawler this turn end." + cont);
        //}
        ReleaseThreadPool();//释放线程池
    }

    public void update() {
        
        synchronized (commandList) {
            this.commandList.clear();
            this.commandList.addAll(constructCommandList());
            if (commandList.size() != 0)
                commandList.notifyAll();
        }
        System.out.println("update:");
        for (Callable<Object> c : commandList) {
            System.out.println((CrawlerCommand) c);
        }
    }

    private List<Callable<Object>> constructCommandList() {

        List<String> words = new LinkedList<>();
        words.add("No1");
        words.add("No2");
        List<Callable<Object>> commandList = new LinkedList<Callable<Object>>();
        for (String word : words) {
            commandList.add(new CrawlerCommand(word));
        }
        return commandList;
    }

}

CrawlerCommand:

package multiThreadPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;

public class CrawlerCommand implements Callable<Object> {
    // private static Logger logger = Logger.getLogger(CrawlerCommand.class);
    private String word;
    private SimpleDateFormat dayformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Object call() {

        
        System.out.println(dayformat.format(new Date())+" 开始:"+word);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(dayformat.format(new Date())+" 结束:"+word);
        return null;
    }

    public CrawlerCommand(String word) {
        this.word = word;
    }

    @Override
    public String toString() {
        return "CrawlerCommand [word=" + word + "]";
    }

}

Observer:

package multiThreadPool;

public interface Observer {
    void update();
}

 

Java多线程实现

标签:

原文地址:http://www.cnblogs.com/zeze/p/5775049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!