线程是操作系统调度的最小单元,也叫轻量级进程。它被包含在进程之中,是进程中的实际运作单位。同一进程可以创建多个线程,每个进程都有自己独立的一块内存空间。并且能够访问共享的内存变量。
进程是资源分配的最小单位,线程是CPU调度的最小单位
大白话说下区别:
public class ThreadCreateOne {
public static void main(String[] args) {
ThreadOne thread = new ThreadOne();
thread.start();
}
}
class ThreadOne extends Thread {
public void run() {
System.out.println("线程已经启动!");
}
}
public class ThreadCteateTwo {
public static void main(String[] args) {
Thread thread = new Thread(new ThreadTwo());
thread.start();
}
}
class ThreadTwo implements Runnable{
@Override
public void run() {
System.out.println("线程已经启动!");
}
}
public class ThreadCreateThree {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask = new FutureTask<Integer>(new ThreadThree());
new Thread(futureTask).start();
Integer integer = futureTask.get();
System.out.println("线程返回结果:" + integer);
}
}
class ThreadThree implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("线程已经执行!");
return 1;
}
}
public static void main(String[] args) {
Thread thread = new Thread();
System.out.println("当前线程状态:"+thread.getState());
}
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
System.out.println("当前线程状态:"+thread.getState());
}
public class ThreadStatus3 {
public static void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (ThreadStatus3.class){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (ThreadStatus3.class) {
}
}
});
thread.start();
Thread.sleep(1000);
System.out.println("当前线程状态:"+thread.getState());
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
LockSupport.park();
}
});
thread.start();
Thread.sleep(500);
System.out.println("当前线程状态:"+thread.getState());
LockSupport.unpark(thread);
Thread.sleep(500);
System.out.println("当前线程状态:"+thread.getState());
}
public static void main(String[] args) throws InterruptedException {
Thread thread3 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread3.start();
Thread.sleep(500);
System.out.println("当前线程状态:"+thread3.getState());
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread();
thread.start();
Thread.sleep(1000);
System.out.println("当前线程状态:"+thread.getState());
}
根据上述的状态,普通线程执行完,就会进入TERMINATED销毁掉,而线程池就是创建一个缓冲池存放线程,执
行结束以后,该线程并不会死亡,而是再次返回线程池中成为空闲状态【阻塞状态】,等候下次任务来临,这使得线程池比手动
创建线程有着更多的优势:
常用的线程池列举:
在线程池的编程模式下,任务是提交给整个线程池,而不是直接提交给某个线程,线程池在拿到任务后,就在内部
协调空闲的线程,如果有,则将任务交给某个空闲的线程。一个线程同时只能执行一个任务,但可以同时向一个线
程池提交多个任务【解释:假设线程池中的A线程接到任务后,会首先处理接到的任务,处理完成后,会先去线程池
中查看是否还有未处理的任务,如果有则会获取任务继续执行,并不会直接进入空闲状态】。
public class ThreadPoolDemo1 {
private static final int CORE_SIZE = 2;
private static final int MAX_SIZE = 2;
private static final int KEEP_ALIVE_TIME = 1;
private static final int QUEUE = 1;
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_SIZE,MAX_SIZE, KEEP_ALIVE_TIME,TimeUnit.SECONDS,new LinkedBlockingDeque<>(QUEUE));
for (int i=0 ;i<5;i++){
executor.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+"线程已经执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。