多线程

作者: 陈小飘 | 来源:发表于2017-04-24 01:53 被阅读0次

    1.继承Thread类

    package xiancheng;
    
    public class learnThread {
    
        //3个线程 主线程 CodingThread DownloadThread
            public static void main(String[] args) {
                long beginTime = System.currentTimeMillis();
                
                Thread cThread = new CodingThread();
                cThread.start();//调度程序
                
                Thread dThread = new DownloadThread();
                dThread.start();//调度程序
                
                long endTime = System.currentTimeMillis();
                long time = endTime - beginTime;
                System.out.println("程序共执行了"+time);
            }
    
    }
    class CodingThread extends Thread{
        public void run() {
            long beginTime = System.currentTimeMillis();
            for(int i=1; i<=100; i++) {
                try {
                    Thread.sleep(10);
                    System.out.println("正在写第"+ i+"行代码");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
            long time = endTime - beginTime;
            System.out.println("CodingThread程序共执行了"+time);
        }
        
    }
    
    class DownloadThread extends Thread{
        public void run() {
            long beginTime = System.currentTimeMillis();
            for(int i=1; i<=100; i++) {
                try {
                    Thread.sleep(10);
                    System.out.println("正在下载第"+ i+"集电视剧");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
            long time = endTime - beginTime;
            System.out.println("DownloadThread程序共执行了"+time);
        }
    }
    

    2.实现Runnable接口的类的实例

    package xiancheng;
    
    public class LearnRunnable {
    
        public static void main(String[] args) {
            
            Thread cThread = new Thread(new CodinThread());
            cThread.start();//调度程序
            
            Thread dThread = new Thread(new DownloaThread());
            dThread.start();//调度程序
        }
    
    }
    class CodinThread implements Runnable{
        public void run() {
            long beginTime = System.currentTimeMillis();
            for(int i=1; i<=100; i++) {
                try {
                    Thread.sleep(10);
                    System.out.println("正在写第"+ i+"行代码");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
            long time = endTime - beginTime;
            System.out.println("CodingThread程序共执行了"+time);
            }
    
    }
    
    class DownloaThread implements Runnable{
        public void run() {
            long beginTime = System.currentTimeMillis();
            for(int i=1; i<=100; i++) {
                try {
                    Thread.sleep(10);
                    System.out.println("正在下载第"+ i+"集电视剧");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
            long time = endTime - beginTime;
            System.out.println("DownloadThread程序共执行了"+time);
        }
    }
    
    1. Callable
    Paste_Image.png Paste_Image.png Paste_Image.png

    sychronized(this)方法/(object)
    cThread.setDeamon(true);守护线程
    cThread.join()
    wait()/notify
    死锁:连个线程同时争夺两个资源,各自获得一个资源,等待另一个
    解决:保证拿资源的顺序是相同的


    有英文基础的同学建议直接学习
    Oracle的官方文档:

    http://docs.oracle.com/javase/tutorial/essential/concurrency/

    多线程的相关问题,大家参考下面的链接:

    http://blog.jobbole.com/76308/

    相关文章

      网友评论

          本文标题:多线程

          本文链接:https://www.haomeiwen.com/subject/lzbyzttx.html