读书人

线程有关别笑

发布时间: 2013-08-01 15:23:18 作者: rapoo

线程相关,别笑
我想程序在执行的过程中去调用别人的方法,并且要传参数过去,但是我不想程序在这里等结果,而是继续往下执行,我知道用线程,但是我不会啊,我现在正在看,希望您能说的明白点,新手,别笑 java线程
[解决办法]

引用:
Quote: 引用:

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());
}
}
}

[解决办法]
//现在有种情况,这里调用的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..");
}
}



读书人网 >J2EE开发

热点推荐