多线程

作者: 常威爆打来福 | 来源:发表于2017-07-08 14:57 被阅读0次
一. 通过继承Thread类实现多线程

Thread类存放于java.lang类库里。java.lang包中提供常用的类,接口,一般异常,系统异常等编程语言核心内容,如基本数据类型,基本数学函数,字符串处理,线程,异常处理类等,系统默认加载这个包,所以我们可以直接使用Thread类。在Thread类中定义了run()方法,要想实现多线程,必须覆写Thread类的run方法。也就是说要使一个类可激活线程,必须按照下面的语法来编写。

class 类名称 extends Thread //从Thread类扩展出子类
{
属性...
方法...
修饰符run() //覆写Thread类里的run方法
{
程序代码; //激活的线程将从run方法开始执行
}
}

案例1:

import Dao.Test;

/**
 * Created by pc on 2017/7/8.
 */
public class ThreadDemo {
    public static void main(String args[]){
        new TestThread().start();
        for(int i=0;i<5;++i){
            System.out.println("main线程在运行");
            try {
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

/**
 * Created by pc on 2017/7/8.
 */
public class TestThread extends Thread {
    public void run(){
        for (int i=0;i<5;++i){
            System.out.println("TestThread在运行");
            try{
            Thread.sleep(1000);
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }
}

运行结果:

案例1运行结果
二. 通过Runnable接口实现多线程

在Java中不允许多继承,即一个子类只有有一个父类,因此如过一个类已经继承了其他类,那么这个类就不能在继承Thread类。此时,,如果一个其他类的子类又想采用多线程技术,那么这时就要用到Runnable接口,来创建线程。接口可以实现多继承。
通过实现Runnable接口实现多线程的语法如下:

class 类名称 implements Runnable //实现Runnable
{
属性...
方法...
public void run() //实现Runnable接口里的run方法
{ //激活的线程将从run方法开始运行
程序代码...
}
}

案例2:

/**
 * Created by pc on 2017/7/8.
 */
public class RunnableThread {
    public static void main(String args[]){
        TestThread1 t=new TestThread1();
        new Thread(t).start();
        for(int i=0;i<5;i++){
            System.out.println("main线程正在运行");
            try{
                Thread.sleep(1000);
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }
}

/**
 * Created by pc on 2017/7/8.
 */
public class TestThread1 implements Runnable {

    public void run() {
    for (int i=0;i<5;i++){
        System.out.println("TestThread在运行");
        try{
            Thread.sleep(1000);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    }
}

运行结果:

案例2运行结果

相关文章

  • 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/uezdhxtx.html