日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java多線程如何防止主線的阻塞

Java多線程在我們盡情的使用中存在著很多的問題,首先就是如何解決主線的阻塞問題。看完下面的代碼希望你對(duì)此有所啟發(fā)。在不斷的學(xué)習(xí)中才能有更好的技術(shù)突破。Java多線程防止主線阻塞代碼如下:

 
 
 
  1. if(e.getSource()==startItem)
  2. {//運(yùn)行多線程爬蟲
  3. stateLabel.setText("運(yùn)行中....");
  4. try {
  5. URL url_url=new URL(web_url);
  6. web_host=url_url.getHost();
  7. } catch (MalformedURLException e2) {
  8. e2.printStackTrace();
  9. }
  10. if(tpool==null)
  11. {
  12. // System.out.println("運(yùn)行");
  13. new Thread(
  14. new Runnable(){ 
  15. public void run() 
  16. {
  17. //要在另一個(gè)線程做得事情
  18. // System.out.println("啦啦啦啦");
  19. tpool=new ThreadPoolExecutor(minN,maxN,aliveT,
  20. TimeUnit.SECONDS,
  21. new LinkedBlockingQueue()/*,
  22. new ThreadPoolExecutor.CallerRunsPolicy()*/);
  23. //editor_pane.setText("創(chuàng)建線程池:
    ");
  24. String tempstr="
    " +
  25. "創(chuàng)建線程池:" +
  26. "
    設(shè)置最小線程數(shù)目:"+minN+
  27. "
    設(shè)置最大線程數(shù)目:"+maxN+
  28. "
    設(shè)置空閑線程存活時(shí)間(秒):"+aliveT+
  29. "
    設(shè)置阻塞線程隊(duì)列大小為:"+blockQN+
  30. "
    " +
  31. "設(shè)置本地映射目錄為:" +
  32. "
    "+dir_path+
  33. "
    " +
  34. "
    爬取入口網(wǎng)頁為:" +
  35. "
    "+web_url+"
    ";
  36. setEditorContent(false,tempstr);
  37. addToUnProQ(web_url);
  38. String geturl=null;
  39. getPage gt=null;
  40. while((!isUnProQEmpty())||!tpool.isTerminated())
    //有沒有處理的連接或者tpool沒有結(jié)束
  41. {//如果 還有連接沒有被處理
  42. geturl=delFromUnProQ();
  43. if(geturl!=null)
  44. {
  45. addToProQ(geturl);
  46. gt=new getPage(geturl);
  47. tpool.execute(gt);
  48. }
  49. try {//等待0.2秒 以便在隊(duì)列中能夠有更多的鏈接
  50. Thread.sleep((long)(Math.random()*200));
  51. } catch (InterruptedException e1) {
  52. e1.printStackTrace();
  53. }
  54. }
  55. tpool.shutdown();
  56. tpool=null;
  57. stateLabel.setText("歡迎使用本網(wǎng)站結(jié)構(gòu)分析軟件");
  58. //setEditorContent(true,shuoming);
  59. }).start();
  60. }
  61. else
  62. {
  63. showMessageBox("已經(jīng)開始運(yùn)行了..不要亂按了.
    閑得慌就到www.51cto.com網(wǎng)站看文章去吧..");
  64. String comd = "explorer http://www.blesslucky.com/";
  65. try {
  66. Runtime.getRuntime().exec(comd);
  67. } catch (IOException e1) {
  68. e1.printStackTrace();
  69. }
  70. }

以上就是對(duì)Java多線程代碼的相關(guān)介紹,希望大家有所收獲。


文章標(biāo)題:Java多線程如何防止主線的阻塞
本文鏈接:http://www.5511xx.com/article/dhgocgi.html