Java基础学习04--进程与线程

首先介绍进程与线程的基本概念

1.进程与线程

进程:正在运行的程序,是系统进行资源分配的基本单位。

线程:线程可以称为轻量级进程,它进程中的一条执行路径,也是CPU的基本调度单位。

一个进程由一个或多个线程组成,彼此间完成不同的工作。多个线程同时工作,称为多线程。

2.进程与线程的区别:

(1)进程是操作系统分配资源的基本单位,线程是CPU调度的基本单位。

(2)一个程序运行至少有一个进程

(3)一个进程可以包含多个线程,但至少有一个线程。

(4)进程间不能共享数据段地址,但同进程的线程之间可以。

3.线程的组成

(1)CPU时间片:操作系统会为每个线程分配执行时间

(2)运行数据:

堆内存:存储线程使用的对象,多个线程可以共享堆中对象。

栈内存:存储线程使用的局部变量,每个线程都拥有独立的栈。

(3)线程的逻辑代码

4.线程的特点

(1线程抢占执行。

(2)并发执行,单核CPU中,是顺序执行(线程切换)。

5.创建线程的四种方式

(1)继承Thread类

(2)实现Runnable接口

(3)实现Callable接口

(4)使用线程池

 1 package com.example.concurrency;
 2 
 3 import java.util.concurrent.Callable;
 4 import java.util.concurrent.ExecutionException;
 5 import java.util.concurrent.Executor;
 6 import java.util.concurrent.ExecutorService;
 7 import java.util.concurrent.Executors;
 8 import java.util.concurrent.Future;
 9 import java.util.concurrent.FutureTask;
10 //第一种:继承Thread类
11 class MyThread extends Thread {
12     public void run() {
13         System.out.println("线程调用");
14     }
15 }
16 
17 //第二种,实现Runnable接口
18 class MyRunnable implements Runnable {
19     private Boolean tag = true;
20 
21     @Override
22     public void run() {
23         // TODO Auto-generated method stub
24         int i = 0;
25         while (tag) {
26             System.out.println("线程调用:" + i++);
27         }
28     }
29 
30     public void stop() {
31         this.tag = false;
32     }
33 }
34 
35 //第三种:实现Callable接口
36 class MyCallable implements Callable<Integer> {
37 
38     @Override
39     public Integer call() throws Exception {
40         // TODO Auto-generated method stub
41         System.out.println("线程调用,返回123");
42         return 123;
43     }
44 
45 }
46 
47 //创建线程的四种方式
48 public class demo05 {
49 
50     public static void main(String[] args) throws InterruptedException, ExecutionException {
51         // 方式一:继承Thread类
52         MyThread thread3 = new MyThread();
53         thread3.start();
54         
55         // 方式二:实现Runnable接口
56         MyRunnable instance = new MyRunnable();
57         Thread thread = new Thread(instance);
58         thread.start();
59 
60         // 方式三-实现Callable接口:
61         MyCallable mc = new MyCallable();
62         FutureTask<Integer> ft = new FutureTask<Integer>(mc);
63         Thread thread2 = new Thread(ft);
64         thread2.start();
65 
66         // 方式四-使用线程池:
67         ExecutorService ser = Executors.newFixedThreadPool(1);// 线程池
68         Future<Integer> ft2 = ser.submit(mc);// 执行线程
69         Integer result = ft2.get();// 获取结果
70         ser.shutdown();// 关闭
71     }
72 }
原文地址:https://www.cnblogs.com/asenyang/p/14166830.html