Este artículo comparte la simple implementación de los ejemplos de Java Multithreading and Thread Pool para su referencia. El contenido específico es el siguiente
1. Dos métodos de implementación de múltiples lectura
1. Clase de hilo heredado
/ *** Implementación simple de la herencia de múltiples subprocesos de la clase de hilo*/ public class Extthread extiende 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 (); nuevo ExtTHread (). Start (); }}}}}2. Implementación de múltiples subprocesos de interfaz ejecutable
/ *** Instancias multithreading que implementan interfaz de runable*/ 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 (); nuevo hilo (rt, "nuevo hilo1"). start (); nuevo hilo (rt, "nuevo hilo2"). start (); }}}}2. Implementación simple de Pool de hilos
// Implementar la clase de interfaz ejecutable implementos testThread 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) {// Crea un grupo de subprocesos con un número fijo de hilos Ejecutorservice Pool = Ejecutors.newFixedThreadPool (5); // enviar tres hilos a la piscina de la piscina de subprocesos. Pool.submit (nuevo testThread ()); Pool.submit (nuevo testThread ()); // Cierre la piscina de hilo.shutdown (); }}3. Los rastreadores de Java usan instancias de piscina de hilos
/ *** Crawler Dispatch Hilo Pool*/ public class Threadpool {public static hashmap <String, Spiders> statusmap = new Hashmap <String, Spiders> (); // almacenar rastreadores, la clave es la identificación de los rastreadores, el valor es el grupo de hilos de rastreadores hashmap static <integer, threadpoolexecutor> threadmap = new Hashmap <Integer, ThreadPoolExeCutor> (); // Crear una piscina de hilo está estática ThreadPoolExExecutor ThreadPool = new ThreadPoolExecutor (200, 230,80000L, TimeUnit.seconds, New ArrayBlockingqueue <Runnable> (10), nuevo ThreadPoolExeCutor.callerrunSpolicy ()); public static void ejecetethread (Spiders Spider) {statusmap.put (String.ValueOf (Spider.getID ()), Spider); // El rastreador es válido if (spider.getflag () == 0) {if (spider.getStatus () == 0) {// indica que el rastreador entra en el estado de rastreo Threadpoolexecutor DetailPool = null; if (threadmap.get (spider.getID ()) == null) {detallpool = new ThreadPoolExecutor (30, 80, 80000L, TimeUnit.Seconds, New ArrayBlockingqueue <Runnable> (10), new ThreadPoolExeCutor.CallerrunPolicy ()); ThreadMap.put (Spider.getID (), Detallepool); threadpool.execute (new ThreadRun (Spider, Threadmap)); }}}}} // Implementar las implementaciones de la clase de interfaz ejecutable ThreadRun Runnable {private Hashmap <Integer, ThreadPoolExeCutor> ThreadPoolMap; arañas privadas arañas; 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 (); }}}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.