Artikel ini membagikan implementasi sederhana contoh multithreading dan thread pool untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1. Dua metode implementasi multithreading
1. Multi-threading mewarisi kelas utas
/ *** Implementasi sederhana dari pewarisan multi-threading kelas utas*/ kelas publik extthread memperluas 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) {exthread baru (). start (); exthread baru (). start (); }}}}}2. Menerapkan multi-threading antarmuka runnable
/ *** Contoh multithreading yang mengimplementasikan antarmuka yang dapat dijalankan*/ kelas publik runthread mengimplementasikan 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 = runthread baru (); utas baru (rt, "utas baru"). start (); utas baru (rt, "thread baru"). start (); }}}}2. Implementasi sederhana dari kumpulan utas
// Menerapkan kelas antarmuka runnable testThread mengimplementasikan runnable {public void run () {for (int i = 0; i <100; i ++) {System.out.println (thread.currentThread (). GetName () + "i is:" + i); }}} kelas publik ThreadPoolTest {public static void main (string [] args) {// Buat kumpulan utas dengan sejumlah utas ExecutorService pool = executors.newfixedThreadPool (5); // Kirim tiga utas ke kumpulan benturan utas. pool.submit (testthread baru ()); pool.submit (testthread baru ()); // tutup thread pool pool.shutdown (); }}3. Java Crawlers Gunakan instance Thread Pool
/ *** Crawler Dispatch Thread Pool*/ Public Class Threadpool {Public Static HashMap <String, Spiders> StatusMap = New HashMap <String, Spiders> (); // Simpan crawler, kuncinya adalah id crawler, nilai adalah kumpulan utas crawler hashmap statis <Integer, threadpoolexecutor> threadMap = hashmap baru <Integer, threadPoolExecutor> (); // Buat thread pool statis threadpoolexecutor threadpool = new ThreadPoolExecutor (200, 230.80000L, timeunit.seconds, arrayblockingqueue baru <Runnable> (10), threadpoolexecutor.callerrunspolicy ()); public static void executeThread (Spiders Spider) {statusMap.put (string.valueof (spider.getId ()), spider); // crawler valid if (spider.getFlag () == 0) {if (spider.getstatus () == 0) {// menunjukkan bahwa crawler memasuki detail threadpoolexecutor state crawling state detailpool = null; if (threadmap.get (spider.getId ()) == null) {detailPool = threadpoolexecutor baru (30, 80, 80000L, timeunit.seconds, arrayblockingqueue baru <Runnable> (10), threadpoolexecutor.callerrunspolicy ()); threadmap.put (spider.getId (), detailPool); threadpool.execute (threadrun baru (laba -laba, threadmap)); }}}}} // Implementasikan implementasi threadrun kelas antarmuka runnable runnable {private hashmap <integer, threadpoolexecutor> threadpoolmap; laba -laba laba -laba pribadi; Public ThreadRun (Spiders Spider, HashMap <Integer, ThreadPoolExecutor> ThreadPoolMap) {this.threadpoolMap = threadPoolMap; this.spider = spider; } // threadExecution body void run () {coba {if ("rong360" .equals (spider.getWebsite ())) {new rongThread (threadpoolmap.get (spider.getid ()), spider) .startspider (); } else if ("xxgg_sd" .equals (spider.getWebsite ()))) {spider_shandong baru (threadpoolmap.get (spider.getid ()), spider) .startspider (); } else if ("xxgg_gz" .equals (spider.getWebsite ()))) {spider_guizhou baru (threadpoolmap.get (spider.getid ()), spider) .startspider (); } else if ("sx" .equals (spider.getWebsite ()))) {new spidersx (spider) .startspider (); } lain jika ("baidu" .Equals (spider.getWebsite ()))) {new spiderbaidu (spider) .startspider (); } else if ("11315" .Equals (spider.getWebsite ())) {new spider11315byname (spider) .startspider (); }} catch (Exception e) {e.printstacktrace (); }}}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.