Cet article partage la simple implémentation des exemples Java Multithreading et Thread Pool pour votre référence. Le contenu spécifique est le suivant
1. Deux méthodes de mise en œuvre de multithreading
1. Multi-threading hérite la classe de threads
/ ** * Implémentation simple de l'héritage multi-threading de la classe de threads * / classe publique Extthread étend 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 exThread (). start (); new ExtThread (). start (); }}}}}2. Implémentation de l'interface multi-lancement de l'interface runnable
/ ** * Instances multithreading qui implémentent l'interface exécutable * / la classe publique Runthread implémente 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 (); Nouveau thread (RT, "New Thread1"). start (); Nouveau thread (RT, "New Thread2"). start (); }}}}2. Implémentation simple du pool de threads
// implément la classe d'interface Runnable TestThread implémente 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) {// Créer un pool de threads avec un nombre fixe de threads ExecutorService pool = exécutors.newFixEdThreadPool (5); // Soumettez trois threads au thread Pool Pool.Submit (new TestThread ()); Pool.Submit (new TestThread ()); Pool.Submit (new TestThread ()); // Fermez le fil de filetage Pool.shutdown (); }}3. Les robots Java utilisent des instances de piscine de fil
/ ** * Crawler Dispatch Thread Pool * / public class Threadpool {public static hashmap <string, spiders> statusmap = new hashmap <String, spiders> (); // Stocker Crawlers, la clé est l'ID des robots, la valeur est le pool de filetage de Crawlers static hashmap <Integer, ThreadPoolExecutor> ThreadMap = new Hashmap <Integer, ThreadPoolExecutor> (); // Créer un thread pool static threadpoolExecutor Threadpool = new ThreadPoolExecutor (200, 230 80000l, timeunit.seconds, new ArrayBlockingQueue <runnable> (10), new ThreadPoolExecutor.CallerRunspolicy ()); public static void exécutEtHread (spiders spider) {statusmap.put (string.valueof (spider.getId ()), spider); // Le robot est valide if (spider.getflag () == 0) {if (spider.getStatus () == 0) {// indique que le robotage entre dans l'état rampant wheadpoolExecutorpool = null; if (threadmap.get (spider.getId ()) == null) {DetailPool = new ThreadPoolExecutor (30, 80, 80000l, timeunit.seconds, new ArrayBlockingQueue <Nuenable> (10), new ThreadPoolExecutor.CallerSerrunSpolicy ()); threadmap.put (spider.getId (), DetailPool); ThreadPool.Exécute (nouveau ThreadRun (Spider, ThreadMap)); }}}}} // implément la classe d'interface runnable implémentations threadrun runnable {private hashmap <Integer, ThreadPoolExecutor> ThreadPoolMap; Spiders privés 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 (); }}}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.