日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程问题—一个线程怎么调出另外一个线程的结果。

發布時間:2024/4/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程问题—一个线程怎么调出另外一个线程的结果。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

問題:建兩線程,線程1進行計算1*1+~+99*99,線程2打印出線程1名字,每隔段時間讀取一次線程1的計算結果?

1 public class Other{ 2 public static volatile int sum; 3 public static void main(String[] args){ 4 Thread t1=new ThreadClass(); 5 t1.setName("我是線程1"); 6 Thread t2=new Thread(new RunnableClass(t1.getName())); 7 t2.setDaemon(true); 8 t1.start(); 9 t2.start(); 10 } 11 static class RunnableClass implements Runnable{ 12 private String name; 13 public RunnableClass(String name){ 14 this.name=name; 15 } 16 public void run(){ 17 while(true){ 18 19 try{ 20 System.out.println(name+" "+sum); 21 Thread.sleep(3000); 22 }catch (Exception e){ 23 System.out.println(e); 24 } 25 } 26 } 27 28 } 29 static class ThreadClass extends Thread { 30 public void run (){ 31 for(int k=1;k<100;k++){ 32 try{sum+=count(k); 33 sleep(1000);}catch(Exception e){ 34 System.out.println(e); 35 } 36 } 37 } 38 public int count(int i){ 39 return (i*i); 40 } 41 42 } 43 }

Runnable接口 沒有 Thread 類里面的方法,所以使用sleep方法時,需要Thread.sleep();記得sleep方法會返回異常,所以要用try來進行異常處理。static的作用!記住,下次將寫出,請大家多提提建議,謝謝!

轉載于:https://www.cnblogs.com/teng-IT/p/4441279.html

總結

以上是生活随笔為你收集整理的线程问题—一个线程怎么调出另外一个线程的结果。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。