下面的程序,可直接运行. 我不明白其run()方法的输出语句, System.out.println((new Thread(this)).currentThread().toString()+sum);
为什么用 new Thread(this)? 按说这又创造了一个线程,但没有启动。
请给解答一下,谢谢!//====
//=========================== class ShareResource
//====
public class ShareResource
{
//------------------- main method
//----
public static void main(String[] args)
{
SumThread number1=new SumThread(1); //创建一个实现了Runnable接口的类的实例。 Thread thread1=new Thread(number1); //创建2个线程。
Thread thread2=new Thread(number1); thread1.start(); //线程启动。
thread2.start();
}//end main
}
//=========================== end ShareResource
////====
//=========================== class SumThread
//====
class SumThread implements Runnable
{
int sum=0;
int num;
//------------------- constructor
//----
public SumThread(int num)
{
this.num=num;
}
//------------------- method run
//----
public void run()
{
for(int i=1;i<5;i++)
{
sum=sum+i;
System.out.println((new Thread(this)).currentThread().toString()+sum);
try
{
Thread.sleep((int)(Math.random()*50));
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}//end try catch
}//end for
System.out.println(sum);
}//end run
}
//=========================== end SumThread

解决方案 »

  1.   

    new Thread(this)主要是为了调用Thread中的currentThread()这个方法
    其实可以直接写Thread.currentThread()是为了获得当时在运行的线程而已。
      

  2.   

    new Tread(this).start();才会启动,currentThread()是一个静态的方法,new出一个对象与直接使用类来调用是一样的。
      

  3.   

    this 本身就是自己的对象,直接this.currentThread()不行吗? 为什么还要new出一个来对象来调用那个静态方法呢?
      

  4.   


    //====
    //=========================== class ShareResource
    //====
    public class ShareResource
    {
    //------------------- main method
    //----
    public static void main(String[] args)
    {
    SumThread number1=new SumThread(1); //创建一个实现了Runnable接口的类的实例。Thread thread1=new Thread(number1); //创建2个线程。
    Thread thread2=new Thread(number1);thread1.start();  //线程启动。
    thread2.start();
    }//end main
    }
    //=========================== end ShareResource
    ////====
    //=========================== class SumThread
    //====
    class SumThread implements Runnable
    {
    int sum=0;
    int num;
    //------------------- constructor
    //----
    public SumThread(int num)
    {
    this.num=num;
    }
    //------------------- method run
    //----
    public void run()
    {
    for(int i=1;i<5;i++)
    {
    sum=sum+i;
    System.out.println((new Thread(this)).currentThread().toString()+sum);
    try
    {
    Thread.sleep((int)(Math.random()*50));
    }
    catch(InterruptedException e)
    {
    System.out.println(e.getMessage());
    }//end try catch
    }//end for
    System.out.println(sum);
    }//end run
    }
    //=========================== end SumThread这样看 会更好些
      

  5.   

    我自己试了,改成this.currentThread()不行,编译会报错.
      

  6.   

    this.currentThread() 肯定不行啊 编译器不能识别当前是否存在线程 
      

  7.   

    仅仅为了调用currentThread(),大量产生无用的Thread类的对象,还是应该用类调用的方法。
    谢谢各位!
      

  8.   

    System.out.println((new Thread(this)).currentThread().toString()+sum);
    严重的问题,new Thread就为去调用 一个 静态方法,thread对象是种特殊的对象,thread对象被new出来后,如果不调用start方法,是永远不会被回收的。
    这个语句说明这人连静态方法是属于类的都不知道害人不浅啊
    直接Thread.currentThread().toString 就可以了
      

  9.   

    线程间通信可以用wait(),notify(),方法。 一个线程因为条件不具备,进入wait()状态,等待使用相同对象的线程运行notify(). 我想问问,在没有运行wait()时,就运行notify(),是不是什么影响都没有?