线程的属性



线程优先级
Java 中线程的优先级范围为 1~10,是一个 int 类型的值 其中最小的优先级 (MIN_PRIORITY) 为 1 正常优先级 (NORM_PRIORITY) 为 5 最高优先级 (MAX_PRIORITY) 为 10 【源码中定义的线程优先级】

线程的状态
Java 中的线程有 6 中状态
线程的状态在源码中定义为枚举类型
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;
}Java 中创建线程的三种方式:
package com.stu.edu.demo1;
public class ThreadDemo1 extends Thread {
public static void main(String[] args) {
ThreadDemo1 thread1 = new ThreadDemo1();
ThreadDemo1 thread2 = new ThreadDemo1();
thread1.setName("THREAD_ONE");
thread2.setName("THREAD_TWO");
thread1.start();
thread2.start();
}
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + " ---------> " + i);
}
}
}package com.stu.edu.demo1;
public class ThreadDemo1 implements Runnable {
public static void main(String[] args) {
ThreadDemo1 run1 = new ThreadDemo1();
ThreadDemo1 run2 = new ThreadDemo1();
Thread thread1 = new Thread(run1,"THREAD_ONE");
Thread thread2 = new Thread(run2,"THREAD_TWO");
thread1.start();
thread2.start();
}
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + " ---------> " + i);
}
}
}在创建线程时调用

package com.stu.edu.demo1;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class ThreadDemo1 implements Callable {
public static void main(String[] args) {
ThreadDemo1 call1 = new ThreadDemo1();
ThreadDemo1 call2 = new ThreadDemo1();
FutureTask<Object> thread1 = new FutureTask<>(call1);
FutureTask<Object> thread2 = new FutureTask<>(call2);
new Thread(thread1,"THREAD_ONE").start();
new Thread(thread2,"THREAD_TWO").start();
}
@Override
public Object call() throws Exception {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + " ---------> " + i);
}
return null;
}
}【call接口代码图片】

FuthureTask 实现了 RunnableFuture 接口,RunableFuture 接口继承了 Runable 接口。

