请教一个多线程问题.

nmyangym 2011-11-02 09:51:26
下面的程序,可直接运行. 我不明白其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
...全文
99 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
nmyangym 2011-11-05
  • 打赏
  • 举报
回复
回16楼, 不行啊?
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
谢谢13楼 !
shaosijun2004 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 nmyangym 的回复:]
线程间通信可以用wait(),notify(),方法。 一个线程因为条件不具备,进入wait()状态,等待使用相同对象的线程运行notify(). 我想问问,在没有运行wait()时,就运行notify(),是不是什么影响都没有?
[/Quote]
无线程可被唤醒,相当于什么也没做
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
请教 5楼,怎么弄成那样那个容易看的?
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
线程间通信可以用wait(),notify(),方法。 一个线程因为条件不具备,进入wait()状态,等待使用相同对象的线程运行notify(). 我想问问,在没有运行wait()时,就运行notify(),是不是什么影响都没有?
shaosijun2004 2011-11-02
  • 打赏
  • 举报
回复
System.out.println((new Thread(this)).currentThread().toString()+sum);
严重的问题,new Thread就为去调用 一个 静态方法,thread对象是种特殊的对象,thread对象被new出来后,如果不调用start方法,是永远不会被回收的。
这个语句说明这人连静态方法是属于类的都不知道。。。。。。害人不浅啊
直接Thread.currentThread().toString 就可以了
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
仅仅为了调用currentThread(),大量产生无用的Thread类的对象,还是应该用类调用的方法。
谢谢各位!
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
this 不是Thread类的对象。
xierong2011 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 nmyangym 的回复:]

我自己试了,改成this.currentThread()不行,编译会报错.
[/Quote]
this.currentThread() 肯定不行啊 编译器不能识别当前是否存在线程
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
我自己试了,改成this.currentThread()不行,编译会报错.
xierong2011 2011-11-02
  • 打赏
  • 举报
回复

//====
//=========================== 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

这样看 会更好些
nmyangym 2011-11-02
  • 打赏
  • 举报
回复
this 本身就是自己的对象,直接this.currentThread()不行吗? 为什么还要new出一个来对象来调用那个静态方法呢?
daijope 2011-11-02
  • 打赏
  • 举报
回复
new Tread(this).start();才会启动,currentThread()是一个静态的方法,new出一个对象与直接使用类来调用是一样的。
小菜鸟的博客 2011-11-02
  • 打赏
  • 举报
回复
顶楼上
qqhw123 2011-11-02
  • 打赏
  • 举报
回复
new Thread(this)主要是为了调用Thread中的currentThread()这个方法
其实可以直接写Thread.currentThread()是为了获得当时在运行的线程而已。
xierong2011 2011-11-02
  • 打赏
  • 举报
回复
写这个里面
xierong2011 2011-11-02
  • 打赏
  • 举报
回复
点击 插入源码
代码写在这里面就可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧