java中如何從主線程傳參數(shù)到子線程
下面這個例子和上面一樣,除了說是子線程要join主線程。本例中還教會了大家,如何從主線程傳參數(shù)到子線程。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
例:1.5.3_2-本章源碼
class ThreadMark_to_win extends Thread {
Thread mainT;
Test t;
public void run() {
try {
mainT.join();
} catch (InterruptedException e) {
System.out.println("我是子程序, 也被打斷");
}
System.out.println("完成"+"e 在子線程"+t.e);
}
public void setMainThread(Thread t1, Test tTest) {
mainT=t1;
t=tTest;
}
}
public class Test {
int e;
public static void main(String[] args) {
Test t=new Test();
Thread mainT = Thread.currentThread();
ThreadMark_to_win tm = new ThreadMark_to_win();
tm.setMainThread(mainT,t);
tm.start();
for (int i = 0; i < 10; i++)
{
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(i==4) tm.interrupt();
t.e = t.e + i;
}
System.out.println("主線程e = " + t.e);
}
}
輸出結(jié)果:
我是子程序, 也被打斷
完成e 在子線程10
主線程e = 45
后續(xù):
馬克-to-win:注意本例中還教會了大家,如何從主線程傳參數(shù)到子線程。
iiii)一個線程捕獲另外一個線程的異常
馬克-to-win:首先我們看子線程如何捕獲子線程自己的異常,和以往一樣。
例:1.5.4_a:
class ThreadMark_to_win extends Thread
{
public void run()
{
for(int i=0;i<3;i++)
{
try {
Thread.sleep(100);
throw new Exception("在子線程,我自己拋出的一個異常");
} catch (InterruptedException e) {
e.printStackTrace();
}catch (Exception ee) {
ee.printStackTrace();
}
System.out.println("在子線程"+i);
}
}
}
public class Test
{
public static void main(String[] args)
{
ThreadMark_to_win tm = new ThreadMark_to_win();
tm.start();
for(int i=0;i<10;i++)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("在主線程"+i);
}
}
}
結(jié)果:
在主線程0
java.lang.Exception: 在子線程,我自己拋出的一個異常
在子線程0
at com.ThreadMark_to_win.run(Test.java:10)
在主線程1
java.lang.Exception: 在子線程,我自己拋出的一個異常
at com.ThreadMark_to_win.run(Test.java:10)
在子線程1
在主線程2
在子線程2
java.lang.Exception: 在子線程,我自己拋出的一個異常
at com.ThreadMark_to_win.run(Test.java:10)
在主線程3
在主線程4
在主線程5
在主線程6
在主線程7
在主線程8
在主線程9
下面我們首先看一下UncaughtExceptionHandler的用法。
馬克-to-win:事實上,UncaughtExceptionHandler可以不用放在一個線程中,只要找到一個類放置就可以了或放置在它自己里面。(以下會給出一個例子),但既然找到一個類放置就可以,我們就可以把他放在另一個線程(一個線程就是一個類)中,達到別的線程處理當(dāng)前線程的異常的目的。
例:1.5.4_0_3-本章源碼
import java.lang.Thread.UncaughtExceptionHandler;
class ThreadMark_to_win {
Thread mainT;
Test test;
ThreadMark_to_win(Thread t1,Test t2)
{
mainT = t1;
test=t2;
mainT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("在子程序處理呢, 傳進來的參數(shù)是"+test.name+" "
+t.getName()+" "+ e.getMessage());
}
});
}
}
public class Test {
String name="馬克-to-win在主線程";
public static void main(String[] args) {
Thread mainT = Thread.currentThread();
Test t=new Test();
ThreadMark_to_win tm = new ThreadMark_to_win(mainT,t);
for (int i = 0; i < 3; i++)
System.out.println("在主線程" + i);
throw new RuntimeException("在主線程,我自己拋出的一個異常");
}
}
輸出結(jié)果:
在主線程0
在主線程1
在主線程2
在子程序處理呢, 傳進來的參數(shù)是馬克-to-win在主線程 main 在主線程,我自己拋出的一個異常
例:1.5.4_0_4
import java.lang.Thread.UncaughtExceptionHandler;
public class Test {
String name="馬克-to-win在主線程";
public static void main(String[] args) {
Thread mainT = Thread.currentThread();
Test t=new Test();
// ThreadMark_to_win tm = new ThreadMark_to_win(mainT,t);
mainT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread th, Throwable e) {
System.out.println("在子程序處理呢, 傳進來的參數(shù)是"+t.name+" "
+th.getName()+" "+ e.getMessage());
}
});
for (int i = 0; i < 3; i++)
System.out.println("在主線程" + i);
throw new RuntimeException("在主線程,我自己拋出的一個異常");
}
}
結(jié)果:
在主線程0
在主線程1
在主線程2
在子程序處理呢, 傳進來的參數(shù)是馬克-to-win在主線程 main 在主線程,我自己拋出的一個異常