joh*_*nny 0 java multithreading
看一些代码,学习线程:
import java.applet.*;
import java.awt.*;
public class CounterThread extends Applet implements Runnable
{
Thread t;
int Count;
public void init()
{
Count=0;
t=new Thread(this);
t.start();
}
public boolean mouseDown(Event e,int x, int y)
{
t.stop();
return true;
}
public void run()
{
while(true)
{
Count++;
repaint();
try {
t.sleep(10);
} catch (InterruptedException e) {}
}
}
public void paint(Graphics g)
{
g.drawString(Integer.toString(Count),10,10);
System.out.println("Count= "+Count);
}
public void stop()
{
t.stop();
}
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中:
public void init() {
Count=0;
t=new Thread(this);
t.start();
}
Run Code Online (Sandbox Code Playgroud)
为什么这个构造函数不会无限期地继续下去?它看起来像是inits,启动一个新的线程传递自己再次调用构造函数(我想),它创建了一个新的线程,等等.
我错过了一些有趣的东西.感谢您的任何帮助
抱歉,我无法使代码看起来正确.出于某种原因,当我将行粘贴到顶部时,不要进入代码解析器.
编辑:谢谢你的答案.为了争论,为了使它成为一个无限循环你会添加它:
t=new Thread(new CounterThread());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
400 次 |
| 最近记录: |