67,513
社区成员
发帖
与我相关
我的任务
分享
t.setDaemon(true);
2 join的时候加上timeout时间
t.join(5000);
//还有上面的代码有什么问题吗?
->没有什么问题
以下是全部代码,doSomething sleep 10s, 但是join的时间是5s,那么在5s之后主线程和子线程都会结束。 5s就是timeout超时时间。5s后子线程还没有醒过来。
import java.lang.reflect.Method;
public class AsyncCall {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// create the thread
final Thread t = new Thread(new Worker(new Object[] { "string1",
"string2" }, new Class[] { String.class, String.class },
OtherClass.class, "doSomething"));
t.setName("async_call_method_thread");
t.setDaemon(true);
System.out.println("ready to call method asynchronous ");
// call the method asynchronize
t.start();
System.out.println("end to call method ");
System.out.println("wait for the method finish");
t.join(5000);
System.out.println("the method has finished");
}
static class Worker implements Runnable {
private final Object[] methodArguments;
private final Class<?>[] methodClazz;
private final Class<?> clazz;
private final String methodName;
private Worker(Object[] arguments, final Class<?>[] methodClazz,
final Class<?> clazz, final String methodName) {
this.methodArguments = arguments;
this.methodClazz = methodClazz;
this.clazz = clazz;
this.methodName = methodName;
}
@Override
public void run() {
try {
final Object o = clazz.newInstance();
final Method m = clazz.getMethod(methodName, methodClazz);
m.setAccessible(true);
m.invoke(o, methodArguments);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
class OtherClass {
/**
* doSomething
*
* @throws InterruptedException
*/
public void doSomething(final String str1, final String str2)
throws InterruptedException {
System.out.println("i am do something, parameter1:" + str1
+ ", parameter2" + str2);
System.out.println("i am ready to sleep 2 sec..");
Thread.sleep(10000);
System.out.println("i has been awaken..");
}
}
public class TestSynchronize {
public static void main(String[] args) {
Object param = new Object();
JobOfOther dd = new JobOfOther(param);// ㊧param
dd.start();
for (int i = 0; i < 5; i++) {
System.out.println("do my job:" + i+param.hashCode());
}
}
}
class JobOfOther extends Thread {
Object param ;
public JobOfOther(Object param) { // ㊧param
this.param = param;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("do other job:" + i +param.hashCode());
}
}
}
import java.lang.reflect.Method;
public class AsyncCall {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// create the thread
final Thread t = new Thread(new Worker(new Object[] { "string1",
"string2" }, new Class[] { String.class, String.class },
OtherClass.class, "doSomething"));
t.setName("async_call_method_thread");
System.out.println("ready to call method asynchronous ");
// call the method asynchronize
t.start();
System.out.println("end to call method ");
System.out.println("wait for the method finish");
t.join();
System.out.println("the method has finished");
}
static class Worker implements Runnable {
private final Object[] methodArguments;
private final Class<?>[] methodClazz;
private final Class<?> clazz;
private final String methodName;
private Worker(Object[] arguments, final Class<?>[] methodClazz,
final Class<?> clazz, final String methodName) {
this.methodArguments = arguments;
this.methodClazz = methodClazz;
this.clazz = clazz;
this.methodName = methodName;
}
@Override
public void run() {
try {
final Object o = clazz.newInstance();
final Method m = clazz.getMethod(methodName, methodClazz);
m.setAccessible(true);
m.invoke(o, methodArguments);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
class OtherClass {
/**
* doSomething
*
* @throws InterruptedException
*/
public void doSomething(final String str1, final String str2)
throws InterruptedException {
System.out.println("i am do something, parameter1:" + str1
+ ", parameter2" + str2);
System.out.println("i am ready to sleep 2 sec..");
Thread.sleep(2000);
System.out.println("i has been awaken..");
}
}
您为什么要join()呢
public class TestSynchronize {
public static void main(String[] args) {
JobOfOther dd = new JobOfOther();
dd.start();
for (int i = 0; i < 100; i++) {
System.out.println("do my job:" + i);
}
}
}
class JobOfOther extends Thread {
public JobOfOther() {
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("do other job:" + i);
}
}
}
import java.lang.reflect.Method;
public class AsyncCall {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// create the thread
final Thread t = new Thread(new Worker(new Object[] { "string1",
"string2" }, new Class[] { String.class, String.class },
OtherClass.class, "doSomething"));
t.setName("async_call_method_thread");
System.out.println("ready to call method asynchronous ");
// call the method asynchronize
t.start();
System.out.println("end to call method ");
System.out.println("wait for the method finish");
t.join();
System.out.println("the method has finished");
}
static class Worker implements Runnable {
private final Object[] methodArguments;
private final Class<?>[] methodClazz;
private final Class<?> clazz;
private final String methodName;
private Worker(Object[] arguments, final Class<?>[] methodClazz,
final Class<?> clazz, final String methodName) {
this.methodArguments = arguments;
this.methodClazz = methodClazz;
this.clazz = clazz;
this.methodName = methodName;
}
@Override
public void run() {
try {
final Object o = clazz.newInstance();
final Method m = clazz.getMethod(methodName, methodClazz);
m.setAccessible(true);
m.invoke(o, methodArguments);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
class OtherClass {
/**
* doSomething
*
* @throws InterruptedException
*/
public void doSomething(final String str1, final String str2)
throws InterruptedException {
System.out.println("i am do something, parameter1:" + str1
+ ", parameter2" + str2);
System.out.println("i am ready to sleep 2 sec..");
Thread.sleep(2000);
System.out.println("i has been awaken..");
}
}