欢迎来到 Java Thread 示例. ** Process** 和 ** Thread** 是执行的两个基本单元。
过程
一个过程是一个自我包含的执行环境,它可以被视为一个程序或应用程序,但一个程序本身包含多个进程。
威胁
线程需要更少的资源来创建,并且存在于进程中,线程共享了进程资源。
Java 线程示例
Every java application has at least one thread - main thread. Although there are so many other java threads running in background like memory management, system management, signal processing etc. But from application point of view - main is the first java thread and we can create multiple threads from it. Multithreading refers to two or more threads executing concurrently in a single program. A computer single core processor can execute only one thread at a time and time slicing is the OS feature to share processor time between different processes and threads.
Java Thread 的优点
与进程相比,Java Threads较轻,创建一个线程需要更少的时间和资源 2线程共享其母进程数据和代码 3线程之间的背景切换通常比进程之间更便宜 4线程交互比进程通信相对容易
Java提供两个方法来编程创建一个线程。
- 实现 java.lang.Runnable接口
- 扩展 java.lang.Thread类
Java Thread Example - 实施可运行界面
要使一个类可运行,我们可以实现 java.lang.Runnable 接口,并在公共空白运行()
方法中提供实现。要将这个类作为线程使用,我们需要通过传输这个可运行类的对象来创建一个线程对象,然后拨打start()
方法以在单独的线程中执行运行()
方法。
1package com.journaldev.threads;
2
3public class HeavyWorkRunnable implements Runnable {
4
5 @Override
6 public void run() {
7 System.out.println("Doing heavy processing - START "+Thread.currentThread().getName());
8 try {
9 Thread.sleep(1000);
10 //Get database connection, delete unused data from DB
11 doDBProcessing();
12 } catch (InterruptedException e) {
13 e.printStackTrace();
14 }
15 System.out.println("Doing heavy processing - END "+Thread.currentThread().getName());
16 }
17
18 private void doDBProcessing() throws InterruptedException {
19 Thread.sleep(5000);
20 }
21
22}
Java Thread 例子 - 扩展 Thread 类
我们可以扩展 java.lang.Thread 类来创建自己的 java 线程类,然后重定义 run()
方法. 然后我们可以创建它的对象,并调用 start()
方法来执行我们自定义的 java 线程类运行方法. 这里有一个简单的 java 线程示例,显示如何扩展 线程类。
1package com.journaldev.threads;
2
3public class MyThread extends Thread {
4
5 public MyThread(String name) {
6 super(name);
7 }
8
9 @Override
10 public void run() {
11 System.out.println("MyThread - START "+Thread.currentThread().getName());
12 try {
13 Thread.sleep(1000);
14 //Get database connection, delete unused data from DB
15 doDBProcessing();
16 } catch (InterruptedException e) {
17 e.printStackTrace();
18 }
19 System.out.println("MyThread - END "+Thread.currentThread().getName());
20 }
21
22 private void doDBProcessing() throws InterruptedException {
23 Thread.sleep(5000);
24 }
25
26}
以下是测试程序,展示如何创建一个Java线程并执行它。
1package com.journaldev.threads;
2
3public class ThreadRunExample {
4
5 public static void main(String[] args){
6 Thread t1 = new Thread(new HeavyWorkRunnable(), "t1");
7 Thread t2 = new Thread(new HeavyWorkRunnable(), "t2");
8 System.out.println("Starting Runnable threads");
9 t1.start();
10 t2.start();
11 System.out.println("Runnable Threads has been started");
12 Thread t3 = new MyThread("t3");
13 Thread t4 = new MyThread("t4");
14 System.out.println("Starting MyThreads");
15 t3.start();
16 t4.start();
17 System.out.println("MyThreads has been started");
18
19 }
20}
上面的 java thread 示例程序的输出是:
1Starting Runnable threads
2Runnable Threads has been started
3Doing heavy processing - START t1
4Doing heavy processing - START t2
5Starting MyThreads
6MyThread - START Thread-0
7MyThreads has been started
8MyThread - START Thread-1
9Doing heavy processing - END t2
10MyThread - END Thread-1
11MyThread - END Thread-0
12Doing heavy processing - END t1
一旦我们开始任何线程,它的执行取决于操作系统的执行时间切割,我们无法控制它们的执行. 然而,我们可以设置线程优先级,但即使如此,它也不保证高优先级线程将首先执行。
无线网络 vs Thread
如果你的类提供更多的功能,而不仅仅是作为线程运行,你应该实施可运行接口以提供一个方法来运行它作为线程. 如果你的类唯一的目标是作为线程运行,你可以扩展线程,然后将结果返回我们的客户端程序,请检查我们的 (Java Callable Future)(/社区/教程/java-callable-future-example)。 **更新:**从Java 8wards开始,Runnable是一个功能接口,我们可以使用lambda表达式来提供其实现,而不是使用匿名类别。