class ThreadDemo
{
public static void main(String[] args) throws Exception
{
Runnable r = new Runnable()
{
public void run()
{
for (int x=0;x<50 ;x++ )
{
//System.out.println(Thread.currentThread().getName()+"........................"+x);
System.out.println(Thread.currentThread().toString()+"........................"+x);
}
}
};
new Thread(r).start();
//new Thread(r).join();//要申请加入,抢夺执行权。join方法必须要声明抛出异常。join可以临时加入线程执行
new Thread(r).setPriority(Thread.MAX_PRIORITY);
new Thread()
{
public void run()
{
for (int x=0;x<100 ;x++ )
{
System.out.println(Thread.currentThread().getName()+"......"+x);
}
}
}.start();
/*
new Thread()
{
public void run()
{
for (int x=0;x<50 ;x++ )
{
System.out.println(Thread.currentThread().getName()+"........................"+x);
}
}
}.start();
*/
for (int x=0;x<100 ;x++ )
{
System.out.println(Thread.currentThread().getName()+".........."+x);
}
}
}
用内部类实现多线程可以不用继承Thread和实现Runnable?是这样的吗?why? |