java实现多线程的方式有三种
继承thread类具备多线程的能力 启动线程 子类对象.start() 不建议使用:避免面向对象单继承的局限性
重写run方法实现 实现接口runnable具备多线程 能力 启动线程 传入目标对+threa对象.start() 推荐使用:避免单继承的局限性,灵活方便,方便同一个对象被多个线程调用
重写call方法,创建线程池,把对象加入到线程池中 启用线程,最后关闭线程池
提示
线程有五种状态:创建、就绪、运行、阻塞、结束
//线程正常停止 //使用标志位 //不要使用stop,destroy
public class TestStop implements Runnable { private boolean flag=true; @Override public void run() { int i=0; while (flag){ System.out.println("run thread"+i++); } } public void stop(){ this.flag=false; } public static void main(String[] args) { TestStop testStop=new TestStop(); new Thread(testStop).start(); for (int i = 0; i < 1000; i++) { System.out.println("main"+i); if (i==900){ testStop.stop(); System.out.println("线程停止"); } } } }
指定当前线程阻塞的毫秒数 存在异常interruptionException 时间到达后进入就绪状态 sleep模拟网络延迟
创建线程最好不要用executors去创建,而使用ThreadPoolExecutors创建,executors返回的线程池创建线程数和最大队列是int的最大值,可能会堆积大量的请求和线程导致oom
public class Demo01 { public static void main(String[] args) { ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程 // ExecutorService threadPool = Executors.newFixedThreadPool(5); // 创建一个固定的线程池的大小 // ExecutorService threadPool = Executors.newCachedThreadPool(); // 可伸缩的,遇强则强,遇弱则弱 try { for (int i = 0; i < 100; i++) { // 使用了线程池之后,使用线程池来创建线程 threadPool.execute(() -> { System.out.println(Thread.currentThread().getName() + " ok"); }); } } catch (Exception e) { e.printStackTrace(); } finally { // 线程池用完,程序结束,关闭线程池 threadPool.shutdown(); } } }
比如现在我要升序排列,返回最后一条数据 如果是mysql的话就可以这样写
select t.* from emr_mrhp.mrhp_log_message t where t.app_no='MRHP_HOME' and t.inp_visit_id='R10005901' order by t.create_date desc limit 1;
在oracle可以先查出排好序的列表,当成一个新表,再通过rownum来限制,如
select * from (select t.* from emr_mrhp.mrhp_log_message t where t.app_no='MRHP_HOME' and t.inp_visit_id='R10005901' order by t.create_date desc) where rownum=1;
这样就实现同样的效果