Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144742
  • 博文数量: 27
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-02 16:13
文章分类

全部博文(27)

文章存档

2009年(18)

2008年(9)

我的朋友

分类: Java

2009-07-24 10:44:50

两种方法创建线程
1.1 扩展Thread,覆盖run()方法
1.2 创建扩展类对象,调用start()方法
class MyThread extends Thread
{
 public void run()
 {
  System.out.println("Hello World");
 }
}
public class HelloWorld {
 public static void main(String[] args) {
  Thread th = new MyThread();
  th.start();
 }
}
 
 
2.1 实现Runnable接口,该类必须实现run()方法
2.2 创建一个该实现类的对象
2.3 用这个对象构造一个Thread对象
2.4 调用Thread对象的start()方法
class MyThread implements Runnable
{
 public void run()
 {
  System.out.println("Hello World!!");
 }
}
public class HelloWorld {
 public static void main(String[] args) {
  MyThread myth = new MyThread();
  Thread th = new Thread(myth);
  th.start();
 }
}
阅读(748) | 评论(0) | 转发(0) |
0

上一篇:servlet hello world

下一篇:hibernate hello world

给主人留下些什么吧!~~