package step1;
//请在此添加实现代码
/********** Begin **********/
public class ThreadClassOne extends Thread{
public int i=0;
public void run()
{
for(int i=0;i<10;i++)
{
if(i%2!=0)
System.out.print(i+" ");
}
}
}
/********** End **********/
package step1;
//请在此添加实现代码
/********** Begin **********/
public class ThreadClassTwo implements Runnable{
public int j=0;
public void run()
{
for(j=0;j<=10;j++)
{
if(j%2==0)
System.out.print(j+" ");
}
}
}
/********** End **********/
第2关:使用 Callable 和 Future 创建线程
package step2;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Task {
public void runThread(int num) {
// 在这里开启线程 获取线程执行的结果
//请在此添加实现代码
/********** Begin **********/
Callable<Integer> callable = new ThreadCallable(num);
FutureTask<Integer> futureTask = new FutureTask<>(callable);
new Thread(futureTask).start();
try {
Integer result = futureTask.get();
System.out.println("线程的返回值为:" + result);
} catch (Exception e) {
e.printStackTrace();
}
/********** End **********/
}
}
/********** Begin **********/
/* 在这里实现Callable接口及方法 */
class ThreadCallable implements Callable<Integer> {
private int num;
public ThreadCallable() {
}
public ThreadCallable(int num) {
this.num = num;
}
public Integer call() throws Exception {
int a,b;
a=1;
b=1;
for (int i = 2; i < num; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
}