// 最简单的 Thread 和 Runnable 的配套使用
Runnable task = () -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
}; // 用 Lambda 表达式实现 Runnable接口
task.run();
Thread thread = new Thread(task);
thread.start();
System.out.println("Done!");
// 带 thread sleep 的 Thread 和 Runnable 的配套使用
Runnable runnable = () -> {
try {
String name = Thread.currentThread().getName();
System.out.println("Foo " + name);
TimeUnit.SECONDS.sleep(1);
System.out.println("Bar " + name);
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
// 最简单的 ExecutorService 使用的例子
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
});
// 以下是ScheduledExecutorService 的使用, 也是ExecutorService的一种
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
// => Hello pool-1-thread-1
// ExecutorService 使用 shutdown 和 shutdownNow 这两个方法去 Interrupt Running线程
try {
System.out.println("attempt to shutdown executor");
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
System.err.println("tasks interrupted");
}
finally {
if (!executor.isTerminated()) {
System.err.println("cancel non-finished tasks");
}
executor.shutdownNow();
System.out.println("shutdown finished");
}
// 使用 Callable的最简单例子, 使用 sleep 可以让thread运行的更加久
Callable<Integer> task = () -> {
try {
TimeUnit.SECONDS.sleep(1);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
};
// 使用 Future 的最简单例子
Runnable task = () -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(task);
System.out.println("future done? " + future.isDone());
Integer result = future.get();
System.out.println("future done? " + future.isDone());
System.out.print("result: " + result);
一共有4种线程池
// Java program to demonstrate thread states
// Java 生命周期的 例子
class thread implements Runnable {
public void run()
{
// moving thread2 to timed waiting state
try {
Thread.sleep(1500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"State of thread1 while it called join() method on thread2 -"
+ Test.thread1.getState());
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test implements Runnable {
public static Thread thread1;
public static Test obj;
public static void main(String[] args)
{
obj = new Test();
thread1 = new Thread(obj);
// thread1 created and is currently in the NEW
// state.
System.out.println(
"State of thread1 after creating it - "
+ thread1.getState());
thread1.start();
// thread1 moved to Runnable state
System.out.println(
"State of thread1 after calling .start() method on it - "
+ thread1.getState());
}
public void run()
{
thread myThread = new thread();
Thread thread2 = new Thread(myThread);
// thread1 created and is currently in the NEW
// state.
System.out.println(
"State of thread2 after creating it - "
+ thread2.getState());
thread2.start();
// thread2 moved to Runnable state
System.out.println(
"State of thread2 after calling .start() method on it - "
+ thread2.getState());
// moving thread1 to timed waiting state
try {
// moving thread1 to timed waiting state
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"State of thread2 after calling .sleep() method on it - "
+ thread2.getState());
try {
// waiting for thread2 to die
thread2.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"State of thread2 when it has finished it's execution - "
+ thread2.getState());
}
}
Thread thread = new Thread(new Runnable);
thread.start(); // thread 被开启, 进入 Runnable 就绪状态
thread.run(); // thread 被分配到CPU时间片后, 就会执行 thread.run() 运行线程
thread.sleep(1); // thread 的 static method, 让thread睡眠, 不释放Lock, 其他thread不能访问
thread.wait(); // thread 的 Object method, 让thread进入等待池, 释放Lock, 其他thread可以访问
// wait() 被使用后, 用 notify() 或者 notifyAll() 进行唤醒
又叫 守护线程, 用 setDaemon(true) 设置; gc thread(垃圾回收线程) 就是一种守护线程; 守护线程在 JVM里面进行工作
CPU上执行多任务,任务状态保存,再加载,这个过程称为上下文切换
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。