请帮忙解释

hnjsnxkjpy 2008-03-16 10:49:25
请兄弟们帮忙说说这每一行代码都是什么意思?

public class ThreadDemo3
{
public static void main(String args[])
{
//new TestThread ().start();
TestThread tt=new TestThread();

Thread t=new Thread(tt);
t.start();
{
System.out.println("main threan is running");
}
}
}
class TestThread implements Runnable //extends Thread
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName()+
"is running");
}
}
}
...全文
94 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2008-03-16
  • 打赏
  • 举报
回复
线程的其中一种构造方式(重载之一)
public Thread(Runnable target)
Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

启动后运行的就是target的run方法
bt_lose 2008-03-16
  • 打赏
  • 举报
回复

package com.test;

/**
*
* @author botao ThreadDemo3 只是一个测试类而已. 其中通过线程的不同实现方式,进行不同的初始化.
*/
public class ThreadDemo3 {
public static void main(String args[]) {
// new TestThread ().start();
Thread t1 = new Thread(new TestThread());// 创建线程1,这中方式创建好象是通过代理的设计模式来实现多线程的.
t1.start();// 开启线程
Thread t2 = new TestThread2();// 创建线程2
t2.start();// 开启线程
// 主现成的任务
for (int i = 0; i < 100; i++) {
System.out.println("主要的" + "is running");
}
}
}

class TestThread implements Runnable {// 实现多现成的第一种方式,实现Runnable接口,实现run方法
/**
* run中写要做的事.
*/
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("次要一一一一一一一一一一一一一" + "is running");
}
}
}

class TestThread2 extends Thread {// 实现多现成的第二种方式,继承Thread,重写run方法.

/**
* run中写要做的事.
*/
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("次要二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二"
+ "is running");
}
}

}


基本上没什么可讲的,不知道对lz有用没??
hnjsnxkjpy 2008-03-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bt_lose 的回复:]
Java code
package com.test;

/**
*
* @author botao ThreadDemo3 只是一个测试类而已. 其中通过线程的不同实现方式,进行不同的初始化.
*/
public class ThreadDemo3 {
public static void main(String args[]) {
// new TestThread ().start();
Thread t1 = new Thread(new TestThread());// 创建线程1,这中方式创建好象是通过代理的设计模式来实现多线程的.
t1.start();// 开启线…
[/Quote]



很感谢谢LZ,给我学习JAVA有很大的鼓励

62,623

社区成员

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

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