直接继承Thread类实现线程的使用,实现方法run(),使用线程对象.start()才会运行线程中的run方法。 class T extends Thread { @Override public void run () { System . out . println ( "ttttt" ); } public static vo
@Override
public void run() {
System.out.println("ttttt");
}
public static void main(String[] args) {
T t = new T();
t.start();
}
}
@Override
public void run() {
System.out.println("ttttt");
}
public static void main(String[] args) {
T t = new T();
new Thread(t).start();
}
}
public static void main(String[] args)
{
CallableThreadTest ctt = new CallableThreadTest();
FutureTask<Integer> ft = new FutureTask<>(ctt);
for(int i = 0;i < 100;i++)
{
System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);
if(i==20)
{
new Thread(ft,"有返回值的线程").start();
}
}
try
{
System.out.println("子线程的返回值:"+ft.get());
} catch (InterruptedException e)
{
e.printStackTrace();
} catch (ExecutionException e)
{
e.printStackTrace();
}
}
@Override
public Integer call() throws Exception
{
int i = 0;
for(;i<100;i++)
{
System.out.println(Thread.currentThread().getName()+" "+i);
}
return i;
}
}