线程算是相对较高级的内容,主要的原因不是说他难,而是它不可见。最近基于多线程的方式优化了一些 FLink 程序,所以这一系列,我们聊聊多线程
进程是计算机系统进行资源分配和调度的最小单位,换句话说我们平时双击那些后缀为 .exe的文件时都会产生一个进程。
进程可以产生若干个线程,是程序执行的最小单位,换句话说,进程就是房子,线程就是房子内一个个干活的人
线程在计算机编程中扮演着重要角色,其重要性主要体现在以下几个方面:
复制代码public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
复制代码Thread thread = new Thread();
thread.start();
在Java中,当调用线程对象的start()
方法时,实际上是调用了start0()
方法,该方法会启动一个新的本地操作系统线程,然后调用Java中的run()
方法来执行线程的任务。所有 线程的主要工作的方法就是 run 方法,那么怎么样来丰富 run 方法的内容呢?
首先通过匿名内部类来启动线程,如:java
复制代码Thread thread1 = new Thread(){
@Override
public void run() {
while (true){
if (Thread.currentThread().isInterrupted()){
System.out.println("Interruted!");
break;
}
Thread.yield();
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1==");
}
}
} ;
thread1.start();
当然也可以实现 Runnable 接口来实现线程的功能java
复制代码public class CreateThread3 implements Runnable {
public static void main(String[] args) {
Thread t1 = new Thread(new CreateThread3());
t1.start();
}
@Override
public void run() {
System.out.println("Oh,I am Runnable");
}
}
复制代码public class ControlledThread extends Thread {
private volatile boolean shouldStop = false;
public void stopThread() {
shouldStop = true;
}
@Override
public void run() {
while (!shouldStop) {
// 线程执行的任务
System.out.println("Thread is running...");
}
System.out.println("Thread stopped.");
}
public static void main(String[] args) {
ControlledThread thread = new ControlledThread();
thread.start();
// 模拟停止线程
try {
Thread.sleep(3000); // 等待3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.stopThread();
}
}
需要注意的是,这里使用 volatile 变量来保证该变量对于任意线程可见,如果不用 volatile 的话,则线程可能会无法停止
复制代码public class InterruptedThread extends Thread {
@Override
public void run() {
while (!Thread.interrupted()) {
// 线程执行的任务
System.out.println("Thread is running...");
}
System.out.println("Thread stopped.");
}
public static void main(String[] args) {
InterruptedThread thread = new InterruptedThread();
thread.start();
// 模拟停止线程
try {
Thread.sleep(3000); // 等待3秒
thread.interrupt(); // 中断线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}