Java中线程的实现方式

Java中线程的实现方式

1.继承Thread类,重写该类的run方法

[线程类 MyThread.java ]

package com.baosight.thread;

/**
 * 继承Thread类,重写该类的run方法
 * @author Administrator
 *
 */
public class MyThread extends Thread{
	
	private int i = 0;
	
	@Override
    public void run() {
        for (i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }


}

[测试类 ThreadTest.java]

package com.baosight.test;

import com.baosight.thread.MyThread;

/**
 * Thread 测试类
 * @author Administrator
 *
 */
public class ThreadTest {

	
	public static void main(String[] args) {
		//创建一个线程
		MyThread myThread=new MyThread();//创建一个新的线程  myThread1  此线程进入新建状态
		//启动一个线程
		myThread.start();//调用start()方法使得线程进入可执行状态
		
		
	}
	
}

[运行结果]

 

Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
Thread-0 8
Thread-0 9
Thread-0 10
Thread-0 11
Thread-0 12
Thread-0 13
Thread-0 14
Thread-0 15
Thread-0 16
Thread-0 17
Thread-0 18
Thread-0 19
Thread-0 20
Thread-0 21
Thread-0 22
Thread-0 23
Thread-0 24
Thread-0 25
Thread-0 26
Thread-0 27
Thread-0 28
Thread-0 29
Thread-0 30
Thread-0 31
Thread-0 32
Thread-0 33
Thread-0 34
Thread-0 35
Thread-0 36
Thread-0 37
Thread-0 38
Thread-0 39
Thread-0 40
Thread-0 41
Thread-0 42
Thread-0 43
Thread-0 44
Thread-0 45
Thread-0 46
Thread-0 47
Thread-0 48
Thread-0 49
Thread-0 50
Thread-0 51
Thread-0 52
Thread-0 53
Thread-0 54
Thread-0 55
Thread-0 56
Thread-0 57
Thread-0 58
Thread-0 59
Thread-0 60
Thread-0 61
Thread-0 62
Thread-0 63
Thread-0 64
Thread-0 65
Thread-0 66
Thread-0 67
Thread-0 68
Thread-0 69
Thread-0 70
Thread-0 71
Thread-0 72
Thread-0 73
Thread-0 74
Thread-0 75
Thread-0 76
Thread-0 77
Thread-0 78
Thread-0 79
Thread-0 80
Thread-0 81
Thread-0 82
Thread-0 83
Thread-0 84
Thread-0 85
Thread-0 86
Thread-0 87
Thread-0 88
Thread-0 89
Thread-0 90
Thread-0 91
Thread-0 92
Thread-0 93
Thread-0 94
Thread-0 95
Thread-0 96
Thread-0 97
Thread-0 98
Thread-0 99

2.实现Runnable接口,并重写该接口的run()方法,该run()方法同样是线程执行体,创建Runnable实现类的实例,并以此实例作为Thread类的target来创建Thread对象,该Thread对象才是真正的线程对象

[线程类 MyRunnable.java]

package com.baosight.thread;

/**
 * 实现Runnable接口,并重写该接口的run()方法
 * @author Administrator
 *
 */
public class MyRunnable implements Runnable {
    private int i = 0;
 
    @Override
    public void run() {
        for (i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}

[测试类 RunnableTest.java]

package com.baosight.test;

import com.baosight.thread.MyRunnable;

/**
 * Runnable 测试类
 * 
 * @author Administrator
 *
 */
public class RunnableTest {

	public static void main(String[] args) {
		Runnable myRunnable = new MyRunnable(); // 创建一个Runnable实现类的对象
		Thread thread1 = new Thread(myRunnable); // 将myRunnable作为Thread
		// target创建新的线程
		thread1.start(); // 调用start()方法使得线程进入就绪状态
	}
}

[测试结果]

Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
Thread-0 8
Thread-0 9
Thread-0 10
Thread-0 11
Thread-0 12
Thread-0 13
Thread-0 14
Thread-0 15
Thread-0 16
Thread-0 17
Thread-0 18
Thread-0 19
Thread-0 20
Thread-0 21
Thread-0 22
Thread-0 23
Thread-0 24
Thread-0 25
Thread-0 26
Thread-0 27
Thread-0 28
Thread-0 29
Thread-0 30
Thread-0 31
Thread-0 32
Thread-0 33
Thread-0 34
Thread-0 35
Thread-0 36
Thread-0 37
Thread-0 38
Thread-0 39
Thread-0 40
Thread-0 41
Thread-0 42
Thread-0 43
Thread-0 44
Thread-0 45
Thread-0 46
Thread-0 47
Thread-0 48
Thread-0 49
Thread-0 50
Thread-0 51
Thread-0 52
Thread-0 53
Thread-0 54
Thread-0 55
Thread-0 56
Thread-0 57
Thread-0 58
Thread-0 59
Thread-0 60
Thread-0 61
Thread-0 62
Thread-0 63
Thread-0 64
Thread-0 65
Thread-0 66
Thread-0 67
Thread-0 68
Thread-0 69
Thread-0 70
Thread-0 71
Thread-0 72
Thread-0 73
Thread-0 74
Thread-0 75
Thread-0 76
Thread-0 77
Thread-0 78
Thread-0 79
Thread-0 80
Thread-0 81
Thread-0 82
Thread-0 83
Thread-0 84
Thread-0 85
Thread-0 86
Thread-0 87
Thread-0 88
Thread-0 89
Thread-0 90
Thread-0 91
Thread-0 92
Thread-0 93
Thread-0 94
Thread-0 95
Thread-0 96
Thread-0 97
Thread-0 98
Thread-0 99

3.使用Callable和Future接口创建线程。具体是创建Callable接口的实现类,并实现call()方法。并使用FutureTask类来包装Callable实现类的对象,且以此FutureTask对象作为Thread对象的target来创建线程

[线程类 MyCallable.java]

package com.baosight.thread;

import java.util.concurrent.Callable;

/**
 * 使用Callable和Future接口创建线程。具体是创建Callable接口的实现类,并实现clall()方法。
 * 并使用FutureTask类来包装Callable实现类的对象,且以此FutureTask对象作为Thread对象的target来创建线程
 * @author Administrator
 *
 */
public class MyCallable implements Callable<Integer> {
    private int i = 0;
 
    
    
    // 与run()方法不同的是,call()方法具有返回值
    @Override
    public Integer call() {
        int sum = 0;
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            sum += i;
        }
        return sum;
    }
}

[测试类 CallableTest.java]

package com.baosight.test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import com.baosight.thread.MyCallable;

/**
 * Callable 测试类
 * @author Administrator
 *
 */
public class CallableTest {
	public static void main(String[] args) {
		 
        Callable<Integer> myCallable = new MyCallable();    // 创建MyCallable对象
        FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask来包装MyCallable对象
 
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                Thread thread = new Thread(ft);   //FutureTask对象作为Thread对象的target创建新的线程
                thread.start();                      //线程进入到就绪状态
            }
        }
 
        System.out.println("主线程for循环执行完毕..");
        
        try {
            int sum = ft.get();            //取得新创建的新线程中的call()方法返回的结果
            System.out.println("sum = " + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
 
    }
}

[测试结果]

main 0
main 1
main 2
main 3
main 4
main 5
main 6
main 7
main 8
main 9
main 10
main 11
main 12
main 13
main 14
main 15
main 16
main 17
main 18
main 19
main 20
main 21
main 22
main 23
main 24
main 25
main 26
main 27
main 28
main 29
main 30
main 31
main 32
main 33
main 34
Thread-0 0
main 35
Thread-0 1
Thread-0 2
main 36
Thread-0 3
main 37
Thread-0 4
main 38
Thread-0 5
main 39
Thread-0 6
main 40
Thread-0 7
main 41
Thread-0 8
main 42
Thread-0 9
main 43
Thread-0 10
main 44
main 45
main 46
main 47
main 48
main 49
Thread-0 11
main 50
Thread-0 12
main 51
Thread-0 13
Thread-0 14
Thread-0 15
main 52
Thread-0 16
main 53
main 54
Thread-0 17
main 55
Thread-0 18
main 56
Thread-0 19
main 57
Thread-0 20
main 58
Thread-0 21
main 59
Thread-0 22
main 60
Thread-0 23
main 61
Thread-0 24
main 62
Thread-0 25
main 63
Thread-0 26
Thread-0 27
main 64
Thread-0 28
Thread-0 29
Thread-0 30
Thread-0 31
Thread-0 32
Thread-0 33
Thread-0 34
Thread-0 35
Thread-0 36
Thread-0 37
Thread-0 38
Thread-0 39
main 65
Thread-0 40
main 66
Thread-0 41
main 67
Thread-0 42
main 68
Thread-0 43
main 69
main 70
main 71
Thread-0 44
main 72
main 73
main 74
Thread-0 45
main 75
main 76
main 77
main 78
main 79
main 80
main 81
main 82
main 83
main 84
main 85
main 86
Thread-0 46
main 87
main 88
main 89
main 90
main 91
main 92
main 93
main 94
Thread-0 47
main 95
main 96
main 97
main 98
Thread-0 48
main 99
主线程for循环执行完毕..
Thread-0 49
Thread-0 50
Thread-0 51
Thread-0 52
Thread-0 53
Thread-0 54
Thread-0 55
Thread-0 56
Thread-0 57
Thread-0 58
Thread-0 59
Thread-0 60
Thread-0 61
Thread-0 62
Thread-0 63
Thread-0 64
Thread-0 65
Thread-0 66
Thread-0 67
Thread-0 68
Thread-0 69
Thread-0 70
Thread-0 71
Thread-0 72
Thread-0 73
Thread-0 74
Thread-0 75
Thread-0 76
Thread-0 77
Thread-0 78
Thread-0 79
Thread-0 80
Thread-0 81
Thread-0 82
Thread-0 83
Thread-0 84
Thread-0 85
Thread-0 86
Thread-0 87
Thread-0 88
Thread-0 89
Thread-0 90
Thread-0 91
Thread-0 92
Thread-0 93
Thread-0 94
Thread-0 95
Thread-0 96
Thread-0 97
Thread-0 98
Thread-0 99
sum = 4950

【总结】

继承Thread和实现Runnable接口实现多线程的区别:

继承Thread类、实现Runnable接口,在程序开发中只要是多线程,肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下优势:

​ 1、可以避免由于Java的单继承特性而带来的局限;

​ 2、增强程序的健壮性,代码能够被多个线程共享,代码与数据是独立的;

​ 3、适合多个相同程序代码的线程区处理同一资源的情况。
 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18620

发表评论