码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的线程实现

时间:2014-09-22 09:19:52      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   ar   div   sp   art   on   

线程实现有两种方法:

1.写一个类来继承Thread类,然后复写run()方法。

public class HelloThread extends Thread {
 
    public void run() {
        System.out.println("Hello from a thread!");
    }
 
    public static void main(String args[]) {
        (new HelloThread()).start();
    }
 
}

 

2.写一个类实现Runnable接口,然后复写run()方法。

public classHelloRunnable implements Runnable {
    public void run() {
        System.out.println("Hello from athread!");
    }
 
    public static void main(String args[]) {
        (new Thread(newHelloRunnable())).start();
    }
 
}

 

Java中的线程实现

标签:style   blog   color   java   ar   div   sp   art   on   

原文地址:http://www.cnblogs.com/tianzhijiexian/p/3985368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!