线程是什么怎么理解它
计算机中执行任务的最小单元,线程是进程的一部分,进程中的一个独立运行的子任务,线程共享进程的资源,包括内存空间,文件句柄和其它共享资源,线程执行是并发的,多个线程可同时执行多个任务,提高程序的性能和资源的利用率
在Java中锁的几种状态实例
public class Thread implements Runnable {
// ...
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;
}
|
- NEW
public static void main(String[] args) {
Thread t = new Thread(() -> {
});
System.out.println(t.getState()); /* NEW */
}
- RUNABLE
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (true) {
}
});
t.start();
System.out.println(t.getState()); /* RUNABLE */
}
- BLOCKED
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Thread t = new Thread(() -> {
synchronized (obj) { /* t 线程拿不到锁资源,导致变为 BLOCKED */
}
});
synchronized (obj) { /* main 线程拿到 obj 的锁资源,并运行 t 线程 */
t.start();
System.out.println(t.getState()); /* BLOCKED */
}
System.out.println(t.getState()); /* BLOCKED */
}
- WATTING
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Thread t = new Thread(() -> {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
Thread.sleep(400);
System.out.println(t.getState()); /* WAITING */
}
- TIMED_WATTING
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t.start();
Thread.sleep(400);
System.out.println(t.getState()); /* TIMED_WAITING */
}
- TERMINATED
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t.start();
Thread.sleep(1000);
System.out.println(t.getState()); /* TERMINATED */
}
创建线程的几种方式
- 通过继承Thread类,重写run方法;
public static void main(String[] args) {
//1.继承Thread
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("继承Thread");
super.run();
}
};
}
- 通过实现runable接口;
public static void main(String[] args) {
//2.实现runable接口
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("实现runable接口");
}
});
thread1.start();
}
- 通过实现callable接口;
线程是什么怎么理解它
计算机中执行任务的最小单元,线程是进程的一部分,进程中的一个独立运行的子任务,线程共享进程的资源,包括内存空间,文件句柄和其它共享资源,线程执行是并发的,多个线程可同时执行多个任务,提高程序的性能和资源的利用率
在Java中锁的几种状态及状态的改变
public class Thread implements Runnable {
// ...
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;
}
|
- NEW
public static void main(String[] args) {
Thread t = new Thread(() -> {
});
System.out.println(t.getState()); /* NEW */
}
- RUNABLE
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (true) {
}
});
t.start();
System.out.println(t.getState()); /* RUNABLE */
}
- BLOCKED
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Thread t = new Thread(() -> {
synchronized (obj) { /* t 线程拿不到锁资源,导致变为 BLOCKED */
}
});
synchronized (obj) { /* main 线程拿到 obj 的锁资源,并运行 t 线程 */
t.start();
System.out.println(t.getState()); /* BLOCKED */
}
System.out.println(t.getState()); /* BLOCKED */
}
- WATTING
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Thread t = new Thread(() -> {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
Thread.sleep(400);
System.out.println(t.getState()); /* WAITING */
}
- TIMED_WATTING
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t.start();
Thread.sleep(400);
System.out.println(t.getState()); /* TIMED_WAITING */
}
- TERMINATED
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t.start();
Thread.sleep(1000);
System.out.println(t.getState()); /* TERMINATED */
}
创建线程的几种方式
- 通过继承Thread类,重写run方法;
public static void main(String[] args) {
//1.继承Thread
Thread t = new Thread() {
@Override
public void run() {
System.out.println("继承Thread");
super.run();
}
};
t.start();
}
- 通过实现runable接口;
public static void main(String[] args) {
//2.实现runable接口
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("实现runable接口");
}
});
thread1.start();
}
- 通过实现callable接口
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(3);
Callable<Object> callable = () -> {
int a = 100;
Thread.sleep(1000);
return a;
};
Future<Object> submit = executor.submit(callable); // 提交任务
System.out.println(submit.get()); // 获取callable放回的值
executor.shutdown(); // 关闭线程池资源
}