zoukankan      html  css  js  c++  java
  • 异步线程的使用

    同步与异步的好处坏处

    1).同步方法卡界面,因为UI线程忙于计算;异步多线程方法不卡界面,主线程闲置,计算任务交个子线程去做;

    2).同步方法慢,只有一个线程计算;异步多线程方法快,多线程并发计算(多线程的资源消耗更多,线程并不是越多越好);

    3).异步多线程是无序的:启动无序,执行时间不确定,结束无序,所以我们不要试图通过启动顺序或是时间等待来控制流程。

    public class SendMailTaskThread {
        //异步线程的调用
        public static void startThread(String subject, String from, String[] to, String text, String filename,String mimeType,String pwd, Date date) {
            System.out.println("ES start,thread id="+Thread.currentThread().getId());
            Runnable createRM = new MyTask(参数);
            new Thread(createRM).start();
            System.out.println("ES shutdown, thread id="+Thread.currentThread().getId());
            
        }
    
    }
    
    class MyTask implements Runnable{
        private String subject;
        
        public MyTask(参数) {
            super();
            this.subject = subject;
        }
    
        @Override
        public void run() {
            System.out.println("creating MyTask, thread id="+Thread.currentThread().getId());
            try {
                try {
                    【具体业务实现】  ----这里写自己的业务  
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end MyTask, thread id="+Thread.currentThread().getId());
        }
    }
  • 相关阅读:
    SVN服务器搭建和使用
    oracle 存储过程
    PLSQL函数
    PL/SQL --> 游标
    PL SQL 游标学习
    PLSQL存储过程
    利用jqueryRotare实现抽奖转盘
    NSString 与NSMutableString的区别
    第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法
    第8章 应用协议 图解TCP/IP 详解
  • 原文地址:https://www.cnblogs.com/Dream2hc/p/java5479442.html
Copyright © 2011-2022 走看看