写的一个简单的例子 非同期调用方法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.."); } }
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); } } }
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()); } } }
* @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..");
}
}
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);
}
}
}
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());
}
}
}
这里只是更好的说明主线程等待子线程的时间段。即开始和结束,就加上了join了。
不join也是可以的。默认是非守护线程。但是如果是守护线程的话,就要join了。
这里只是更好的说明主线程等待子线程的时间段。即开始和结束,就加上了join了。
不join也是可以的。默认是非守护线程。但是如果是守护线程的话,就要join了。
public class sss { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
final Thread t = new Thread(new Worker("张三", "123456"));
t.start();
System.out.println("这里是我接下来要执行的代码");
} static class Worker implements Runnable {
private final String userName;
private final String userpwd; private Worker(String username, String userpwd) {
this.userName = username;
this.userpwd = userpwd;
} @Override
public void run() {
//这里我去调用别人的方法
aaa a = new aaa();
String result = a.test(userName);
//现在有种情况,这里调用的test()存在调用超时的情况,意思就是对方服务器没有响应 结果
//那么我现在就要做一个调用超时的处理,请问我这里怎么写
//还有上面的代码有什么问题吗?
}
}}
//那么我现在就要做一个调用超时的处理,请问我这里怎么写1 设置为线程为守护线程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..");
}
}