多线程

作者: 陈小飘 | 来源:发表于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/

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

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