我想输出原神,启动和王者,启动但是我的控制台输出确是main启动,
代码如下
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
class CallableClass implements Callable<Object> {
@Override
public Object call() throws Exception {
return "null";
}
}
class Demo4{
public static void main(String[] args) {
CallableClass c1 = new CallableClass();
FutureTask<Object> futureTask = new FutureTask<>(c1);
Thread thread = new Thread(futureTask,"原神");
thread.start();
System.out.println(Thread.currentThread().getName()+"启动!");
FutureTask<Object> futureTask1 = new FutureTask<>(c1);
Thread thread1 = new Thread(futureTask1,"王者");
thread1.start();
System.out.println(Thread.currentThread().getName()+"启动!");
}
}
有人能讲讲怎么回事吗?我是根据Runnable学的将对象添加到Thread的构造方法里就能够为这个线程起一个名字,类似于这样
class RunnableClass implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行!");
}
}
class Demo3{
public static void main(String[] args) {
RunnableClass runnableClass= new RunnableClass();
//使用Thread的构造方法创建对象
Thread thread = new Thread(runnableClass,"thread666");
thread.start();
}
}
控制台输出