Thread常见的构造方法
方法 | 说明 |
---|---|
Thread() | 创建线程对象 |
Thread(String name) | 创建线程对象,并命名 |
Thread(Runnable targe) | 使用Runnable对象创建线程对象 |
Thread(Runnable targe,String name) | 使用Runnable对象创建线程对象,并命名 |
Thread(ThreadGroup group ,Runnable targe) | 可以分组来管理线程,且使用到了Runnable类 |
Thread(ThreadGroup group ,Runnable targe,String name) | 可以分组来管理线程,且使用到了Runnable类,并命名 |
1.使用Thread类创建
public class Thread1 {
public static void main(String[] args) {
//在主函数中。不new一个线程,直接去得到当前的线程,为主线程;
Thread t=Thread.currentThread();
System.out.println("线程为"+t.getName());//线程名:main 这是系统自己为主线程定义的名
//创建一个线程并初始化
Thread thread=new Thread(){
//具体的业务代码
@Override
public void run(){
//使用Thread.currentThread()得到当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName());//线程名:Thread-0 自己未命名时,系统会去命名,当有多个线程时,依次为Thread-0、Thread-1、Thread-2....
}
};
//开启线程
thread.start();
}
}
2.继承Thread 创建线程
/**
* 继承Thread 创建线程
*/
class MyThread2 extends Thread{
//具体的业务代码
@Override
public void run(){
//使用Thread.currentThread()得到当前线程
Thread thread=Thread.currentThread();
System.out.println("线程名:"+thread.getName());//线程名:Thread-0
}
}
public class Thread2 {
public static void main(String[] args) {
//创建一个新线程
Thread thread=new MyThread2();
thread.start();
}
}
3.继承Runnable接口创建线程
/**
* 继承Runnable接口创建线程
*/
class MyThread3 implements Runnable{
@Override
//具体的业务代码
public void run(){
//使用Thread.currentThread()得到当前线程
Thread thread=Thread.currentThread();
System.out.println("线程名:"+thread.getName());//线程名:123
}
}
public class Thread3 {
public static void main(String[] args) {
MyThread3 mythread3=new MyThread3();
//给线程起名为123
Thread thread=new Thread(mythread3,"123");
thread.start();
}
}
4.Runnable匿名内部类创建线程
/**
* Runnable匿名内部类创建线程
*/
public class Thread4 {
public static void main(String[] args) {
//匿名内部类
Thread thread=new Thread(new Runnable() {
//具体的业务代码
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程为"+t.getName());
}
},"线程4");//给线程起名为线程4
thread.start();
}
}
5.使用lambda创建线程
/**
* 使用lambda创建线程
*/
public class Thread5 {
public static void main(String[] args) {
Thread thread=new Thread(()->{
//使用Thread.currentThread()得到当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName());
},"线程5");//给线程起名为线程5
thread.start();
}
}
6.使用Callable创建线程,可创建任意数据类型的线程
/**
* 使用Callable创建线程;Callable<v>泛型
*/
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
//得到当前线程.再得到线程名
System.out.println(Thread.currentThread().getName());
}
}
public class Thread6 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建callable实例
MyCallable myCallable=new MyCallable();
//接收callable结果的对象
FutureTask<Integer> futureTask=new FutureTask<>(myCallable);
//创建一个新线程
Thread thread=new Thread(futureTask,"线程6");//给线程起名为线程6
thread.start();
}
}
7.使用FutureTask创建
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Thread7 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> futureTask=new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName());
}
});
//创建一个新线程
Thread thread=new Thread(futureTask,"线程7");//给线程起名为线程7
thread.start();
}
}
8.创建线程组
public class Thread_group {
public static void main(String[] args) throws InterruptedException {
//创建一个线程分组
ThreadGroup threadGroup=new ThreadGroup("女子组100米");
//定义任务
Runnable run=new Runnable() {
@Override
public void run() {
//随机给运动员一个跑步成绩(score)范围为[0,500)
int score=new Random().nextInt(500);
try {
//线程休眠;score越大,线程休眠时间越长,比赛成绩越靠后
Thread.sleep(score);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" "+score);
}
};
//3个运动员都是threadGroup组的,需要完成Runnable类的run任务,并分别给命名为1、2、3
Thread thread1=new Thread(threadGroup,run,"1");
Thread thread2=new Thread(threadGroup,run,"2");
Thread thread3=new Thread(threadGroup,run,"3");
//开始比赛
thread1.start();
thread2.start();
thread3.start();
}
}
执行结果如下图: