この記事では、参照用のJavaマルチスレッドとスレッドプールの例の簡単な実装を共有しています。特定のコンテンツは次のとおりです
1。マルチスレッドの2つの実装方法
1.マルチスレッドはスレッドクラスを継承します
/ ***スレッドクラスのマルチスレッド継承の簡単な実装*/ public class extthread extends 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(); new extthread()。start(); }}}}}2。実行可能なインターフェイスのマルチスレッドの実装
/ ***実行可能なインターフェースを実装するマルチスレッドインスタンス}} 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。スレッドプールの簡単な実装
//実行可能なインターフェイスクラス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){//固定数のスレッドexecutorservice pool = executors.newfixedthreadpool(5); // 3つのスレッドをスレッドプールに送信するpool.submit(new testThread()); pool.submit(new testthread()); pool.submit(new testthread()); //スレッドプールプールを閉じます。shutdown(); }}3. Java Crawlersは、スレッドプールインスタンスを使用します
/ ***クローラーディスパッチスレッドプール*/パブリッククラススレッドプール{public static hashmap <string、spiders> statusmap = new hashmap <string、spiders>(); //クローラーを保存し、キーはクローラーのID、値はクローラーのスレッドプールであるstatic hashmap <integer、threadpoolexecutor> threadmap = new hashmap <integer、threadpoolexecutor>(); //スレッドプールの作成StatiC StreadPoolexecutorスレッドプール= new ThreadPoolexecutor(200、230、80000L、TimeUnit.Seconds、new ArrayBlockingQueue <Runnable>(10)、new SthreadPoolexecutor.callerrunspolicy(); public static void executEthread(spiders spider){statusmap.put(string.valueof(spider.getId())、spider); //クローラーは有効です(spider.getflag()== 0){if(spider.getStatus()== 0){//クローラーがクロール状態のスレッドプールエクセクターdefaultpool = nullに入ることを示します。 if(threadmap.get(spider.getId())== null){detailpool = new SthreadPoolexecutor(30、80、80000L、TimeUnit.seconds、new ArrayBlockingQueue <Runnable>(10)、new SthreadPoolexecutor.CallerrunSpolicy()); threadmap.put(spider.getId()、detailpool); threadpool.execute(new Streadrun(Spider、ThreadMap)); }}}}} //実行可能なインターフェイスクラススレッドラン実装を実装してください{private hashmap <integer、threadpoolexecutor> threadpoolmap;プライベートスパイダースパイダー。 public Streadrun(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(例外e){e.printstacktrace(); }}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。