This article shares the simple implementation of Java multithreading and thread pool examples for your reference. The specific content is as follows
1. Two implementation methods of multithreading
1. Multi-threading inherits Thread class
/** * Simple implementation of multi-threading inheritance of Thread class*/ public class extThread extends Thread { public void run(){ for(int i=0;i<100;i++){ System.out.println(getName()+"-"+i); } } public static void main(String arg[]){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); if(i==50){ new extThread().start(); new extThread().start(); } } } } }2. Implementing multi-threading of Runnable interface
/** * Multithreading instances that implement runable interface*/ public class runThread implements Runnable { public void run(){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); } } public static void main(String arg[]){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); if(i==50){ runThread rt = new runThread(); new Thread(rt,"New Thread1").start(); new Thread(rt,"New Thread2").start(); } } } }2. Simple implementation of thread pool
//Implement the Runnable interface class TestThread implements Runnable{ public void run() { for(int i = 0;i < 100;i++){ System.out.println(Thread.currentThread().getName() + "i is: " + i); } } } public class threadPoolTest { public static void main(String[] args) { //Create a thread pool with a fixed number of threads ExecutorService pool = Executors.newFixedThreadPool(5); //Submit three threads to the thread pool pool.submit(new TestThread()); pool.submit(new TestThread()); pool.submit(new TestThread()); //Close the thread pool pool.shutdown(); } }3. Java crawlers use thread pool instances
/** * Crawler dispatch thread pool*/ public class threadPool { public static HashMap<String, Spiders> statusMap = new HashMap<String, Spiders>(); // Store crawlers, key is the id of crawlers, value is the thread pool of crawlers static HashMap<Integer, ThreadPoolExecutor> threadMap = new HashMap<Integer, ThreadPoolExecutor>(); //Create a thread pool static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 230,80000L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy()); public static void executeThread(Spiders spider) { statusMap.put(String.valueOf(spider.getId()), spider); // The crawler is valid if (spider.getFlag() == 0) { if (spider.getStatus() == 0) { // Indicates that the crawler enters the crawling state ThreadPoolExecutor detailPool = null; if (threadMap.get(spider.getId()) == null) { detailPool = new ThreadPoolExecutor(30, 80, 80000L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>( 10), new ThreadPoolExecutor.CallerRunsPolicy()); threadMap.put(spider.getId(), detailPool); threadPool.execute(new threadRun(spider, threadMap)); } } } } } //Implement the Runnable interface class threadRun implementations Runnable { private HashMap<Integer, ThreadPoolExecutor> threadPoolMap; private Spiders spider; public threadRun(Spiders spider, HashMap<Integer, ThreadPoolExecutor> threadPoolMap) { this.threadPoolMap = threadPoolMap; this.spider = spider; } //ThreadExecution body public void run() { try { if ("rong360".equals(spider.getWebsite())) { new RongThread(threadPoolMap.get(spider.getId()), spider) .startSpider(); } else if ("xxgg_sd".equals(spider.getWebsite()))) { new Spider_ShanDong(threadPoolMap.get(spider.getId()), spider).startSpider(); } else if ("xxgg_gz".equals(spider.getWebsite()))) { new Spider_GuiZhou(threadPoolMap.get(spider.getId()), spider).startSpider(); } else if ("sx".equals(spider.getWebsite()))) { new SpiderSX(spider).startSpider(); } else if ("baidu".equals(spider.getWebsite()))) { new SpiderBaiDu(spider).startSpider(); } else if ("11315".equals(spider.getWebsite()))) { new Spider11315ByName(spider).startSpider(); } } catch (Exception e) { e.printStackTrace(); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.