1.继承Thread类;
2.重写run()方法;
3.创建线程对象,调用start()方法
/**
* 自定义线程
*/
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
}
public static void main(String[] args) {
//创建线程对象
MyThread thread1 = new MyThread();
//启动线程
thread1.start();
//主线程执行
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
}
}
常见的问题:
1.执行run()方法和start()方法有什么区别?
执行run方法是在主线程中同步执行的,start()是去启动新的线程
2.那么调用两次start()方法会怎么样呢?
会抛出异常IllegalThreadStateException,线程是一次性的,不允许执行两次
3。多线程的执行是什么顺序?
上下文切换
多线程的执行是抢占式的,线程会去抢占CPU,抢到后执行自己的指令,执行过程中CPU可能被其它线程抢占,其它线程执行
上下文切换回原来线程时,如何执行从哪里开始执行?
每个线程有自己的程序计数器,保存当前线程执行的行数,切换回来后继续执行下面的行代码
2.实现runnable接口
1.定义的类实现runnable接口
2.实现run方法
3.创建Thread对象,传入Runnable对象,调用start()方法
/**
* 自定线程 Runnable方式
*/
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
}
public static void main(String[] args) {
//创建Runnable对象 创建Thread对象
Thread thread1 = new Thread(new MyRunnable());
//启动线程
thread1.start();
//匿名内部类
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
}
});
thread2.start();
//lambda表达式
Thread thread3 = new Thread(()->{
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
});
thread3.start();
}
}
常见的问题
1. 继承Thread类和实现Runnable两种方式的区别
1> Java是单继承的,继承Thread类就不能继承其它类,实现接口没有此限制
2> 继承Thread不强制要求重写run,实现Runnable强制要求
3> Runnable可以使用Lambda表达式,语法简介
推荐使用Runnable方式
3.实现Callable接口
前面两种方式都实现的run方法没有返回值,如果需要进行运算后返回值,就需要使用Callable接口
1) 实现Callable接口的call方法
2) 创建FutureTask对象传入Callable实现对象
3) 创建Thread线程传入FutureTask对象
4) 启动线程
5) 通过FutureTask的get方法获得返回值
/**
* 带返回值的线程对象
*/
public class MyCallable implements Callable<Long> {
@Override
public Long call() throws Exception {
long sum = 0;
for(int i = 0;i < 10000000;i++){
sum += i;
}
return sum;
}
public static void main(String[] args) {
//创建FutureTask对象,传入Callable对象
FutureTask<Long> futureTask = new FutureTask<>(new MyCallable());
//创建Thread对象
Thread thread = new Thread(futureTask);
//启动线程
thread.start();
//获得返回值
try {
Long value = futureTask.get();
System.out.println("运算结果是:" + value);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}