تشترك هذه المقالة في التنفيذ البسيط لأمثلة Java Multithreading و Pool Pool للرجوع إليها. المحتوى المحدد كما يلي
1.
1.
/ *** التنفيذ البسيط للميراث متعدد الخيوط لفئة مؤشرات الترابط*/ تمديد الفئة العامة يمتد مؤشر الترابط {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. تنفيذ متعدد الخيوط للواجهة القابلة للتشغيل
/ *** مثيلات multithreading التي تنفذ واجهة قابلة للتشغيل*/ تنفيذ RunThRead من الفئة العامة تنفذ 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 () ؛ موضوع جديد (RT ، "New Thread1"). start () ؛ موضوع جديد (RT ، "New Thread2"). start () ؛ }}}}2. التنفيذ البسيط لمجموعة الخيوط
. }}} الفئة العامة threadpooltest {public static void main (string [] args) {// قم بإنشاء تجمع مؤشرات ترابط مع عدد ثابت من مؤشرات الترابط pool executorservice = eventors.NewFixedThreadPool (5) ؛ // إرسال ثلاثة مؤشرات ترابط إلى تجمع الخيوط. pool.submit (new testthread ()) ؛ pool.submit (new testthread ()) ؛ // أغلق بركة المسبح. shutdown () ؛ }}3.
/ *** Crawler Dispatch Thread Pool*/ Public Class ThreadPool {public static hashmap <string ، Spiders> statusMap = new hashmap <string ، spiders> () ؛ // زحف المتجر ، المفتاح هو معرف الزحف ، القيمة هي تجمع مؤشرات الترابط من HashMap الثابتة <integer ، threadpoolexecutor> threadmap = new hashmap <integer ، threadpoolexecutor> () ؛ // قم بإنشاء تجمع مؤشر ترابط ثابت threadpoolexecutor threadpool = new threadpoolexecutor (200 ، 230،80000L ، timeUnit.Seconds ، ArrayBlockingqueue <Runnable> (10) ، ThreadPoolexecutor.callerrunspolicy ()) ؛ Public Static Void ExecuteThread (Spiders Spider) {statusMap.put (string.valueof (Spider.getId ()) ، Spider) ؛ // الزاحف صالح إذا كان (spider.getflag () == 0) {if (spider.getStatus () == 0) {// يشير إلى أن الزاحف يدخل في حالة تفاصيل الحالة الزاحف sthowpoolexecutor = null ؛ if (threadmap.get (spider.getId ()) == null) {detailpool = new threadpoolexecutor (30 ، 80 ، 80000L ، timeUnit.seconds ، arrayblockingqueue new <Runnable> (10) ، threadpoolexecutor.callerrunspolicy ()) ؛ ThreadMap.put (Spider.getId () ، التفاصيل) ؛ threadpool.execute (ThreadRun جديد (Spider ، threadMap)) ؛ }}}}} // تنفيذ تطبيقات سلسلة واجهة Runnable Runnable {private hashmap <integer ، threadpoolexecutor> threadpoolmap ؛ العنكبوت الخاص عنكبوت. Public ThreadRun (العنكبوت العنكبوت ، hashmap <integer ، threadpoolexecutor> threadpoolmap) {this.threadpoolmap = threadpoolmap ؛ this.spider = Spider ؛ }. } آخر إذا ("xxgg_sd" .equals (spider.getWebsite ())))) {new Spider_shandong (threadpoolmap.get (Spider.getId ()) ، spider) .startspider () ؛ } آخر إذا ("xxgg_gz" .equals (spider.getWebsite ())))) {new Spider_Guizhou (threadpoolmap.get (spider.getid ()) ، spider) .startspider () ؛ } آخر إذا ("sx" .equals (Spider.getWebsite ())))) {new SpidersX (Spider) .StartSpider () ؛ } آخر إذا ("baidu" .equals (spider.getWebsite ())))) {new SpiderBaidu (Spider) .StartSpider () ؛ } آخر إذا ("11315" .equals (spider.getWebsite ())))) }} catch (استثناء e) {E.PrintStackTrace () ؛ }}}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.