Este artigo compartilha a implementação simples dos exemplos de Java multithreading e threads pool para sua referência. O conteúdo específico é o seguinte
1. Dois métodos de implementação de multithreading
1. Classe de threads de herda multithreading
/ *** Implementação simples da herança multi-threading da classe de thread*/ classe pública Extthread estende o 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 (); novo extthread (). start (); }}}}}2. Implementando a interface multi-threading da interface executável
/ *** Instâncias multithreading que implementam interface executável*/ public class RunThread implementa 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 (); novo thread (rt, "novo thread1"). start (); novo thread (rt, "new Thread2"). start (); }}}}2. Implementação simples do pool de threads
// Implementar a classe de interface executável Testthread implementa Runnable {public void run () {for (int i = 0; i <100; i ++) {System.out.println (thread.currentThread (). GetName () + "i é:" + i); }}} classe pública threadpooltest {public static void main (string [] args) {// crie um pool de threads com um número fixo de threads executorService pool = executores.newfixedthreadpool (5); // Envie três threads para o pool de threads. pool.submit (new testthread ()); pool.submit (new testthread ()); // fechar o pool de threads pool.shutdown (); }}3. Os rastreadores Java usam instâncias do pool de threads
/ *** Pool de threads de despacho de crawler*/ public class Threadpool {public static hashmap <string, Spiders> statusMap = new Hashmap <String, Spiders> (); // Armazenar rastreadores, chave é o ID dos rastreadores, o valor é o pool de threads de rastreadores estáticos hashmap <inteiro, threadpoolExecutor> threadmap = new hashmap <inteiro, threadpoolexecutor> (); // Crie um pool de threads static threadpoolExecutor threadpool = new ThreadPoolExecutor (200, 230.80000L, timeUnit.Seconds, novo ArrayBlockingQueue <uncrnable> (10), novo ThreadpoolExecutor.CallerRunSpolicy ()); public static void executethread (aranhas aranha) {statusmap.put (string.valueof (spider.getId ()), spider); // o rastreador é válido se (spider.getflag () == 0) {if (spider.getStatus () == 0) {// indica que o rastreador entra no rastreamento de threadpoolExecutor detalhepool = null; if (threadmap.get (spider.getId ()) == null) {detalhepool = new ThreadpoolExecutor (30, 80, 80000l, timeUnit.Seconds, novo ArrayBlockingQueue <dunnable> (10), new ThreadPoolexecutor.CallerRunSpolicy ()); Threadmap.put (spider.getId (), detalhepool); Threadpool.execute (novo Threadrun (Spider, Threadmap)); }}}}} // Implemente a classe Runnable Interface implementações ThreadRun Runnable {private hashmap <Inteiro, ThreadPoolExecutor> threadpoolmap; aranha privada aranha; public threadrun (aranhas aranha, hashmap <inteiro, threadpoolExecutor> threadpoolmap) {this.threadpoolmap = threadpoolmap; this.spider = aranha; } // threadExecution body public void run () {tente {if ("rong360" .equals (spider.getwebsite ())) {new Rongthread (shaderpoolmap.get (spider.getId ()), spider) .stspider (); } 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 (Exceção e) {e.printStackTrace (); }}}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.