精华内容
下载资源
问答
  • 线程优先级

    2020-12-29 15:27:22
    每一个线程都是有优先级的,一般来说,高优先级的线程在...线程优先级是一个int变量(从1-10),1代表最低优先级,10代表最高优先级。 java的线程优先级调度会委托给操作系统去处理,所以与具体的操作系统优先级有...

    每一个线程都是有优先级的,一般来说,高优先级的线程在运行时会具有优先权,但这依赖于线程调度的实现,这个实现是和操作系统相关的(OS dependent)。

    我们可以定义线程的优先级,但是这并不能保证高优先级的线程会在低优先级的线程前执行。线程优先级是一个int变量(从1-10),1代表最低优先级,10代表最高优先级。

    java的线程优先级调度会委托给操作系统去处理,所以与具体的操作系统优先级有关,如非特别需要,一般无需设置线程优先级。

    展开全文
  • java线程池多线程优先级 Java线程优先级 (Java Thread Priorities) Priority of a thread describes how early it gets execution and selected by the thread scheduler. In Java, when we create a thread, ...

    java线程池多线程优先级

    Priority of a thread describes how early it gets execution and selected by the thread scheduler. In Java, when we create a thread, always a priority is assigned to it. In a Multithreading environment, the processor assigns a priority to a thread scheduler. The priority is given by the JVM or by the programmer itself explicitly. The range of the priority is between 1 to 10 and there are three constant variables which are static and used to fetch priority of a Thread. They are as following:

    线程的优先级描述了线程调度程序提早执行和选择线程的时间。 在Java中,当我们创建线程时,总是为其分配优先级。 在多线程环境中,处理器将优先级分配给线程调度程序。 优先级由JVM或程序员本身明确指定。 优先级的范围在1到10之间,并且有三个常量,它们是静态的,用于获取线程的优先级。 它们如下:

    1. public static int MIN_PRIORITY (1. public static int MIN_PRIORITY)

    It holds the minimum priority that can be given to a thread. The value for this is 1.

    它拥有可以赋予线程的最低优先级。 该值为1。

    2. public static int NORM_PRIORITY (2. public static int NORM_PRIORITY)

    It is the default priority that is given to a thread if it is not defined. The value for this is 0.

    如果未定义,则它是赋予线程的默认优先级。 该值为0。

    3. public static int MAX_PRIORITY (3. public static int MAX_PRIORITY)

    It is the maximum priority that can be given to a thread. The value for this is 10.

    它是可以赋予​​线程的最大优先级。 该值为10。

    线程优先级中的Get和Set方法 (Get and Set methods in Thread priority)

    1. public final intgetPriority() (1. public final intgetPriority())

    In Java, getPriority() method is in java.lang.Thread package. it is used to get the priority of a thread.

    在Java中,getPriority()方法位于java.lang.Thread包中。 它用于获取线程的优先级。

    2. public final void setPriority(intnewPriority) (2. public final void setPriority(intnewPriority))

    In Java setPriority(intnewPriority) method is in java.lang.Thread package. It is used to set the priority of a thread. The setPriority() method throws IllegalArgumentException if the value of new priority is above minimum and maximum limit.

    在Java中,setPriority(intnewPriority)方法在java.lang.Thread包中。 它用于设置线程的优先级。 如果新优先级的值高于最小和最大限制,则setPriority()方法将引发IllegalArgumentException。

    示例:获取线程优先级 (Example: Fetch Thread Priority)

    If we don’t set thread priority of a thread then by default it is set by the JVM. In this example, we are getting thread’s default priority by using the getPriority() method.

    如果我们不设置线程的线程优先级,则默认情况下由JVM设置。 在此示例中,我们通过使用getPriority()方法获取线程的默认优先级。

    class MyThread extends Thread 
    { 
    	public void run() 
    	{ 
    		System.out.println("Thread Running..."); 
    	} 
    
    	public static void main(String[]args) 
    	{ 
    		MyThread p1 = new MyThread(); 
    		MyThread p2 = new MyThread(); 
    		MyThread p3 = new MyThread(); 
    		p1.start();
    		System.out.println("P1 thread priority : " + p1.getPriority()); 
    		System.out.println("P2 thread priority : " + p2.getPriority());  
    		System.out.println("P3 thread priority : " + p3.getPriority()); 
    		
    	} 
    }

    P1 thread priority : 5 Thread Running... P2 thread priority : 5 P3 thread priority : 5

    P1线程优先级:5线程运行中... P2线程优先级:5 P3线程优先级:5

    示例:线程常量 (Example: Thread Constants)

    We can fetch priority of a thread by using some predefined constants provided by the Thread class. these constants returns the max, min and normal priority of a thread.

    我们可以使用Thread类提供的一些预定义常量来获取线程的优先级。 这些常量返回线程的最大,最小和普通优先级。

    class MyThread extends Thread 
    { 
    	public void run() 
    	{ 
    		System.out.println("Thread Running..."); 
    	} 
    
    	public static void main(String[]args) 
    	{ 
    		MyThread p1 = new MyThread(); 
    		p1.start();
    		System.out.println("max thread priority : " + p1.MAX_PRIORITY); 
    		System.out.println("min thread priority : " + p1.MIN_PRIORITY);  
    		System.out.println("normal thread priority : " + p1.NORM_PRIORITY); 
    		
    	} 
    }

    Thread Running... max thread priority : 10 min thread priority : 1 normal thread priority : 5

    线程运行中...最大线程优先级:10最小线程优先级:1正常线程优先级:5

    示例:设置优先级 (Example : Set Priority)

    To set priority of a thread, setPriority() method of thread class is used. It takes an integer argument that must be between 1 and 10. see the below example.

    要设置线程的优先级,请使用线程类的setPriority()方法。 它采用必须在1到10之间的整数参数。请参见以下示例。

    class MyThread extends Thread 
    { 
    	public void run() 
    	{ 
    		System.out.println("Thread Running..."); 
    	} 
    
    	public static void main(String[]args) 
    	{ 
    		MyThread p1 = new MyThread();
    		// Starting thread
    		p1.start();
    		// Setting priority
    		p1.setPriority(2);
    		// Getting priority
    		int p = p1.getPriority();
    		
    		System.out.println("thread priority : " + p);  
    		
    	} 
    }

    thread priority : 2 Thread Running...

    线程优先级:2线程正在运行...

    例: (Example: )

    In this example, we are setting priority of two thread and running them to see the effect of thread priority. Does setting higher priority thread get CPU first. See the below example.

    在此示例中,我们设置两个线程的优先级并运行它们以查看线程优先级的效果。 设置更高优先级的线程是否首先获取CPU。 请参见以下示例。

    class MyThread extends Thread 
    { 
    	public void run() 
    	{ 
    		System.out.println("Thread Running... "+Thread.currentThread().getName()); 
    	} 
    
    	public static void main(String[]args) 
    	{ 
    		MyThread p1 = new MyThread();
    		MyThread p2 = new MyThread();
    		// Starting thread
    		p1.start();
    		p2.start();
    		// Setting priority
    		p1.setPriority(2);
    		// Getting -priority
    		p2.setPriority(1);
    		int p = p1.getPriority();
    		int p22 = p2.getPriority();
    		
    		System.out.println("first thread priority : " + p);  
    		System.out.println("second thread priority : " + p22);
    		
    	} 
    }

    Thread Running... Thread-0 first thread priority : 5 second thread priority : 1 Thread Running... Thread-1

    线程运行中...线程-0第一个线程优先级:5第二个线程优先级:1线程运行中...线程-1

    Note: Thread priorities cannot guarantee that a higher priority thread will always be executed first than the lower priority thread. The selection of the threads for execution depends upon the thread scheduler which is platform dependent.

    注意:线程优先级不能保证高优先级的线程总是比低优先级的线程优先执行。 执行线程的选择取决于平台相关的线程调度程序。

    翻译自: https://www.studytonight.com/java/thread-priorities-in-java.php

    java线程池多线程优先级

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 269,524
精华内容 107,809
关键字:

线程优先级