
多线程
北方有梦_hsp
这个作者很懒,什么都没留下…
展开
-
多线程-四种线程池
缓存线程池(长度无限制) 任务加入后的执行流程 判断线程池是否存在空闲线程 存在则使用 不存在,则创建线程,并放入线程池,然后使用 创建与使用 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test1 { public static void main(String[] args) { //指挥线程池执行新的任务原创 2020-08-19 15:36:07 · 431 阅读 · 0 评论 -
多线程-Callable线程创建方式
Runnable 与 Callable //接口定义 //Callable接口 public interface Callable<V> { V call() throws Exception; } //Runnable接口 public interface Runnable { public abstract void run(); } Callable使用步骤 编写类实现Callable接口 , 实现call方法 class XXX implements Callable<T原创 2020-08-18 21:53:28 · 223 阅读 · 0 评论 -
多线程-进程通信
进程通信 Object.wait方法(休眠) Object.notifyAll和notify(唤醒) 生产者与消费者 public class Demo12 { public static void main(String[] args) { //多线程通信 生产者与消费者问题 Food f = new Food(); new Cook(f).start(); new Waiter(f).start();原创 2020-08-18 21:29:22 · 158 阅读 · 0 评论 -
多线程-线程安全问题
线程安全问题 什么是线程安全问题? 答:看下面这个例子,有时候执行出来余票会出现-1或-2,虽然我们已经在循环的时候限制了余票(count>0),但是依然会出现这个问题,这就是线程安全问题。 //因线程不安全,出现问题的案例 public class Demo7 { public static void main(String[] args) { //线程不安全 Runnable run = new Ticket(); new Thread(ru原创 2020-08-18 17:58:51 · 146 阅读 · 2 评论 -
多线程-继承Thread和实现Runnable
继承Thread实现多线程 实现多线程需要继承Thread类,并实现run方法 class MyThread extends Thread{ @Override public void run() { //这里的代码 就是一条执行路径。 } } run方法就是线程要执行的任务方法,这个执行路径的触发方式不是调用run方法,而是通过Thread对象的start()来启动任务 public static void main(String[] args) { MyThread原创 2020-08-18 06:08:19 · 642 阅读 · 0 评论