线程

如何创建和启动一个线程

  1. 定义子类,继承Thread。重新定义方法run
package cn.hiluna.day26;

/**
 * 定义子类,继承Thread
 * 重新方法run
 */
public class SubThread extends Thread{
    public void run(){
        for (int i =0 ; i < 50;i++){
            System.out.println("run..."+ i);
        }
    }
}

  1. 创建Thread子类对象,子类对象调用方法start(),让线程程序执行,JVM调用线程中的run
package cn.hiluna.day26;

/**
 * 如何创建和启动一个线程
 *  创建Thread子类对象
 *  子类对象调用方法start()
 *      让线程程序执行,JVM调用线程中的run
 */
public class ThreadDemo {
    public static void main(String[] args) {
        SubThread subThread = new SubThread();
        subThread.start();
        for (int i =0 ; i < 50;i++){
            System.out.println("main..."+ i);
        }
    }
}

程序运行的效果图:

%title插图%num

注意:如果subThread直接调用run方法及

SubThread subThread = new SubThread();
        subThread.run();
        for (int i =0 ; i < 5;i++){
            System.out.println("main..."+ i);
        }

那么此程序就为单线程程序。执行结果如下图:

%title插图%num


多线程执行时,在内存中的运行图解:

%title插图%num


每个线程都有自己的名字
  • 运行方法main线程,名字就是“main”无法更改
  • 其他新建线程也有名字,默认“Thread-0,Thread-1”

线程子类:

package cn.hiluna.day26;

public class NameThread extends Thread {
    @Override
    public void run() {
        System.out.println(0/0);
    }
}

创建Thread子类对象:

package cn.hiluna.day26;

/**
 * 每个线程都有自己的名字
 * 运行方法main线程,名字就是“main”无法更改
 * 其他新建线程也有名字,默认“Thread-0,Thread-1”
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        NameThread nameThread = new NameThread();
        nameThread.start();

        NameThread nameThread0 = new NameThread();
        nameThread0.start();
    }
}

可以从报的错误中看出线程的名字:

%title插图%num


在父类Thread方法中提供了一个方法可以获取线程的名字:

%title插图%num

在父类Thread方法中提供了一个方法可以设置线程的名字:

%title插图%num

在Thread的构造方法中,也有一个构造器可以设置线程的名称

%title插图%num

如何在子类构造器中访问父类构造器:
通过super("小强");来访问父类构造器。

package cn.hiluna.day26;

/**
 * 获取线程的名字,父类Thread方法
 *  String  getName()
 */
public class NameThread extends Thread {

    public NameThread(){
        super("小强");
    }

    @Override
    public void run() {
        System.out.println(getName());
    }
}


如何获取main这个线程的线程名称:

JVM开启了 主线程来运行方法main,主线程也是线程,是线程必然就是Thread类对象
* Thread类中,定义了一个静态方法
* static Thread currentThread()返回正在执行的线程对象

System.out.println(Thread.currentThread().getName());

使用匿名内部类,实现多线程程序
* 前提:继承或者接口的实现
* new 父类或者new 接口(){
* 重新抽象方法

package cn.hiluna.day26.ThreadDemo04;

/**
 * 使用匿名内部类,实现多线程程序
 * 前提:继承或者接口的实现
 * new 父类或者new 接口(){
 *     重新抽象方法
 * }
 */
public class ThreadDemo {
    public static void main(String[] args) {
        //继承方式 xxx extends Thread{ public void run(){}}
        new Thread(){
            public void run(){
                System.out.println("!!!");
            }
        }.start();
        //实现接口方式 xxx implements Runnable{ public void run(){}}
        Runnable runnable = new Runnable(){
            public void run(){
                System.out.println("###");
            }
        };
        new Thread(runnable).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("@@@");
            }
        }).start();
    }
}

实现线程程序的另一个方法,接口实现


  1. 实现接口Runnable,重写run方法
package cn.hiluna.day26.ThreadDemo03;

/**
 * 实现线程程序的另一个方法,接口实现
 * 实现接口Runnable,重新run方法
 */
public class SubRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0 ; i < 50 ;i++){
            System.out.println("run..."+i);
        }
    }
}

  1. 创建Thread类对象,构造方法中传递Runnable接口实现类
  2. 调用Thread类方法start()
package cn.hiluna.day26.ThreadDemo03;

import cn.hiluna.day26.SubThread;

/**
 * 实现接口方式的线程
 *  创建Thread类对象,构造方法中传递Runnable接口实现类
 *  调用Thread类方法start()
 */
public class ThreadDemo {
    public static void main(String[] args) {
        SubRunnable subThread = new SubRunnable();
        Thread thread = new Thread(subThread);
        thread.start();
        for (int i = 0;i < 50; i++){
            System.out.println("main..."+i);
        }
    }
}


使用匿名内部类,实现多线程程序
* 前提:继承或者接口的实现
* new 父类或者new 接口(){
* 重新抽象方法

package cn.hiluna.day26.ThreadDemo04;

/**
 * 使用匿名内部类,实现多线程程序
 * 前提:继承或者接口的实现
 * new 父类或者new 接口(){
 *     重新抽象方法
 * }
 */
public class ThreadDemo {
    public static void main(String[] args) {
        //继承方式 xxx extends Thread{ public void run(){}}
        new Thread(){
            public void run(){
                System.out.println("!!!");
            }
        }.start();
        //实现接口方式 xxx implements Runnable{ public void run(){}}
        Runnable runnable = new Runnable(){
            public void run(){
                System.out.println("###");
            }
        };
        new Thread(runnable).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("@@@");
            }
        }).start();
    }
}

实现线程程序的另一个方法,接口实现


  1. 实现接口Runnable,重写run方法
package cn.hiluna.day26.ThreadDemo03;

/**
 * 实现线程程序的另一个方法,接口实现
 * 实现接口Runnable,重新run方法
 */
public class SubRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0 ; i < 50 ;i++){
            System.out.println("run..."+i);
        }
    }
}

  1. 创建Thread类对象,构造方法中传递Runnable接口实现类
  2. 调用Thread类方法start()
package cn.hiluna.day26.ThreadDemo03;

import cn.hiluna.day26.SubThread;

/**
 * 实现接口方式的线程
 *  创建Thread类对象,构造方法中传递Runnable接口实现类
 *  调用Thread类方法start()
 */
public class ThreadDemo {
    public static void main(String[] args) {
        SubRunnable subThread = new SubRunnable();
        Thread thread = new Thread(subThread);
        thread.start();
        for (int i = 0;i < 50; i++){
            System.out.println("main..."+i);
        }
    }
}


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇