线程概述
Java程序中的执行顺序或流程在读取时称为。
线程也称为轻量级进程,因为它们共享相同的数据和进程地址空间。
线程优先级
每个线程都有一个优先级,基于该优先级,它会获得执行优先级。线程的优先级范围为[1…10]。
下面是在Thread类中定义的一些常量的列表,用于指定线程优先级:
java.lang.Thread.MIN_PRIORITY = 1
java.lang.Thread.NORM_PRIORITY = 5
java.lang.Thread.MAX_PRIORITY = 10
线程调度
Java运行时使用固定优先级调度算法来调度线程。
通常,具有较高优先级的线程(但是不能保证)在具有较低优先级的线程之前执行。
但是,JVM可能决定在较高优先级的线程之前执行较低优先级的线程,以防止饥饿。线程可以随时通过调用该Thread.yield()方法来决定放弃其执行权。
线程只能将CPU分配给具有相同优先级的其他线程,但是如果一个线程试图让其分配给较低优先级的线程,则该请求将被忽略。
螺纹类型
线程有两种,即用户线程和守护程序线程。
用户线程—用户线程是普通线程,通常用于运行我们的程序代码。
守护程序线程—守护程序线程也称为服务提供者线程。它们通常用于执行一些系统代码。如果没有用户线程在运行,JVM将终止守护程序线程。因此,不应将守护程序线程用于执行我们的程序逻辑。
线程的生命周期
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
如何Thread 在Java中使用类?
下面是Thread在Java中使用类的示例。
// defining a thread class MyThread extends Thread { public void run() { // actions to be performed within thread // . . . System.out.println("Inside MyThread"); } } class Main { public static void main(String[] args) { // creating a new thread and starting it MyThread t = new MyThread(); t.start(); // changes the thread priority t.setPriority(Thread.MAX_PRIORITY); // t.setPriority(Thread.MIN_PRIORITY); try { // causes the thread to temprorarily stop execution t.sleep(1000); // 1000 milliseconds } catch (InterruptedException e) { e.getMessage(); } System.out.println("Inside main method"); } }
如何Runnable在Java中使用接口?
以下是使用该Runnable界面的示例。
// implementing a thread class MyRunnable implements Runnable { public void run() { // actions to be performed within thread // ... System.out.println("Inside MyThread"); } } public class Main { public static void main(String[] args) { // creating and starting a new thread using runnable Runnable runnable = new MyRunnable(); Thread t = new Thread(runnable); t.start(); System.out.println("Inside main method"); // changes the thread priority t.setPriority(Thread.MAX_PRIORITY); //t.setPriority(Thread.MIN_PRIORITY); try { // causes the thread to temprorarily stop execution t.sleep(1000); // 1000 milliseconds } catch (InterruptedException e) { e.getMessage(); } System.out.println("Inside main method"); } }
如何使用Runnable带有匿名类的接口?
通过使用接口的匿名 类实现,我们可以进一步简化上述代码片段Runnable。
请参阅下面的代码片段以了解这一点。
public class Main { public static void main(String[] args) { // creating and starting a new thread using annonymous class Runnable runnable = new Runnable () { public void run() { // actions to be performed within thread // ... System.out.println("Inside MyThread"); } }; Thread t = new Thread(runnable); t.start(); System.out.println("Inside main method"); // changes the thread priority t.setPriority(Thread.MAX_PRIORITY); //t.setPriority(Thread.MIN_PRIORITY); try { // causes the thread to temprorarily stop execution t.sleep(1000); // 1000 milliseconds } catch (InterruptedException e) e.getMessage(); } } }
如何Runnable在Lambda表达式中使用实现?
在Java 8或更高版本中,Runnable可以通过使用lambda表达式作为Runnable接口来进一步简化线程的实现。
请参阅下面的示例以了解如何实现此目的。
public class Main { public static void main(String[] args) { // creating and starting a new thread using annonymous class Runnable runnable = () -> { // actions to be performed within thread // ... System.out.println("Inside MyThread"); }; Thread t = new Thread(runnable); t.start(); System.out.println("Inside main method"); // changes the thread priority t.setPriority(Thread.MAX_PRIORITY); //t.setPriority(Thread.MIN_PRIORITY); try { // causes the thread to temprorarily stop execution t.sleep(1000); // 1000 milliseconds } catch (InterruptedException e) { e.getMessage(); } } }
由于代码的可读性和简洁性,因此这是最可取的方法。
结论 首选使用Runnable接口,因为由于Java中缺乏对多继承的支持,因此需要扩展Thread类的第一种方法将MyThread类限制为进一步扩展另一个类。
原文链接:http://codingdict.com