---
title: "Thread与Runnable"
author: "jackyIT"
date: "2010-01-27T09:39:24Z"
tags: []
categories: ["编程语言"]
slug: "thread-runnable"
draft: false
lastmod: "2010-01-27T09:39:24Z"
id: 580718
url: "https://jackyit.iteye.com/blog/580718"
---

一、进程与线程的区别
    在多任务系统中，比如Windows，linux，unix中，每个执行的程序就是一个进程。
    线程其实比进程更细化，可以这么说一个或多个线程组成一个进程，这里提到了一个线程或多个线程，其实也就是单线程和多线程，两者的区别就是一个有序一个无序，请看下面的实例：
1.单线程的实现

```java
 
 public class ThreadTest1 {
    public void run(){
       while(true){
          System.out.prinln(Thread.currentThread().getName()+"Jacky is my english name!");
       }
    }
 }
@Test
public class Test {
    public static void main(String str[]){
       new ThreadTest1().run();
       while(true){
          System.out.prinln(Thread.currentThread().getName()+"I am Jacky");
       }
    }
 }

@result: 因为单线程是有序的，所以永远只输出 main Jacky is my english name
```


2.多线程的实现


```java
 
 public class ThreadTest1 extends Thread {
    public void run(){
       while(true){
          System.out.prinln(Thread.currentThread().getName()+"Jacky is my english name!");
       }
    }
 }
@Test
public class Test {
    public static void main(String str[]){
       new ThreadTest1().start();
       while(true){
          System.out.prinln(Thread.currentThread().getName()+"I am Jacky");
       }
    }
 }

@parse: 1.继承了Thread线程类。2.调用start();而不是run()方法。
@result: 分别交替输出 main Jacky is my english name 和 main I am Jacky
```