从头认识多线程-1.1 多线程的创建

本文详细介绍了Java中多线程的创建方法,包括通过继承Thread类和实现Runnable接口两种方式。通过示例代码展示了如何使用默认构造器和带有线程名的构造器创建线程,以及使用Runnable接口实现多线程的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章节我们来简单回顾一下多线程的创建,其实这个在之前的从头认识java的系列文章里面已经聊到过这个话题,在这里重新梳理一遍,当成这个系列的开篇。

1.继承Thread

Thread的构造器

Thread()
Allocates a new  Thread object.
Thread(Runnable target)
Allocates a new  Thread object.
Thread(Runnable target, String name)
Allocates a new  Thread object.
Thread(String name)
Allocates a new  Thread object.
Thread(ThreadGroup group, Runnable target)
Allocates a new  Thread object.
Thread(ThreadGroup group, Runnable target, String name)
Allocates a new  Thread object so that it has  target as its run object, has the specified  name as its name, and belongs to the thread group referred to by  group.
Thread(ThreadGroup group, Runnable target, String name, long stackSize)
Allocates a new  Thread object so that it has  target as its run object, has the specified  name as its name, and belongs to the thread group referred to by  group, and has the specified  stack size.
Thread(ThreadGroup group, String name)
Allocates a new  Thread object.
Thread的构造器里面可以放入几种类型的参数,我这里只是展示默认的和Thread(String name)这两个,后面会慢慢的把其他的也融入到文章里面


(1)使用默认的构造器

package com.ray.deepintothread.ch01.topic_1;

public class SimpleExtendThread {
	public static void main(String[] args) {
		ThreadOne threadOne = new ThreadOne();
		Thread thread = new Thread(threadOne);
		System.out.println("start");
		thread.start();
	}
}

class ThreadOne extends Thread {
	@Override
	public void run() {
		System.out.println("begin to run");
		super.run();
		System.out.println("end to run");
	}
}

输出:

start
begin to run
end to run


(2)使用带名字的构造器

package com.ray.deepintothread.ch01.topic_1;

public class SimpleExtendThread2 {
	public static void main(String[] args) {
		ThreadOne2 threadOne = new ThreadOne2("线程A");
		Thread thread = new Thread(threadOne);
		System.out.println("start");
		thread.start();
	}
}

class ThreadOne2 extends Thread {

	private String name;

	public ThreadOne2(String name) {
		this.name = name;
	}

	@Override
	public void run() {
		System.out.println("Thread " + name + " begin to run");
		super.run();
		System.out.println("Thread " + name + " end to run");
	}
}

start
Thread 线程A begin to run
Thread 线程A end to run


2.实现Runnable

package com.ray.deepintothread.ch01.topic_1;

public class SimpleRunnableImpl {
	public static void main(String[] args) {
		RunnableOne threadOne = new RunnableOne();
		Thread thread = new Thread(threadOne);
		System.out.println("start");
		thread.start();
	}
}

class RunnableOne implements Runnable {

	@Override
	public void run() {
		System.out.println("begin to run");
	}
}

输出:

start
begin to run


由于java是单根继承,因此,我们更多的时候会使用实现Runnable的方式来实现多线程。


总结:这一章节简单的回顾一下多线程的创建。


我的github:https://github.com/raylee2015/DeepIntoThread


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值