方式一:继承Thread目的重写run方法
1.自定义一个类继承Thread类
2.重新Thread类的run方法
重写run方法的目的是什么?
为了把自定义线程的任务代码写在run方法中。
每个线程都有自己的任务代码,jvm创建的主线程的任务代码就是main方法中的所有代码, 自定义线程的任务代码就写在run方法中,自定义线程负责了run方法中代码。
3. 创建Thread的子类对象,并且调用start方法开启线程。
注意: 一个线程一旦开启,那么线程就会执行run方法中的代码,run方法千万不能直接调用,直接调用run方法就相当调用了一个普通的方法而已
并没有开启新的线程。
public class Demo1 extends Thread {
@Override //把自定义线程的任务代码写在run方法中
public void run() {
// TODO Auto-generated method stub
for(int i =0;i<100;i++){
System.out.println("自定义线程:"+i);
}
}
public static void main(String[] args) {
//创建了自定义的线程对象。
Demo1 d = new Demo1();
//调用start方法启动线程
d.start();
for(int i = 0 ; i < 100 ; i++){
System.out.println("main线程:"+i);
}
}
}
/*
需求: 模拟QQ视频与聊天同时在执行。
*/
class TalkThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("hi,你好!开视频呗...");
}
}
}
class VideoThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("视频视频....");
}
}
}
public class Demo2 {
public static void main(String[] args) {
TalkThread talkThread = new TalkThread();
talkThread.start();
VideoThread videoThread = new VideoThread();
videoThread.start();
}
}
推荐使用: 第二种。 实现Runable接口的。
原因: 因为java单继承 ,多实现的。
方式二: 实现Runnable接口,实现run方法
1. 自定义一个类实现Runnable接口。
2. 实现Runnable接口 的run方法,把自定义线程的任务定义在run方法上。
3. 创建Runnable实现类对象。
4. 创建Thread类 的对象,并且把Runnable实现类的对象作为实参传递。
5. 调用Thread对象 的start方法开启一个线程。
public class mydemo implements Runnable{ @Override public void run() { System.out.println("this:"+ this); System.out.println("当前线程:"+ Thread.currentThread()); } public static void main(String[] args) { //创建Runnable实现类的对象 mydemo d = new mydemo(); //创建Thread类的对象, 把Runnable实现类对象作为实参传递。 Thread thread = new Thread(d,"狗娃"); //Thread类使用Target变量记录了d对象, //调用thread对象的start方法开启线程。 thread.start(); } /* Thread类 的run方法 * @Override public void run() { if (target != null) { target.run(); //就相当于Runnable实现类的对象的run方法作为了Thread对象的任务代码了。 } } */ }
输出结果:
this:cn.itcast.thread.mydemo@72ebbf5c
当前线程:Thread[狗娃,5,main]
public class Demo3 implements Runnable{ @Override public void run() { // System.out.println("this:"+ this); // System.out.println("当前线程:"+ Thread.currentThread()); for(int i = 0 ; i < 100 ; i++){ System.out.println(Thread.currentThread().getName()+":"+i); } } public static void main(String[] args) { //创建Runnable实现类的对象 Demo3 d = new Demo3(); //创建Thread类的对象, 把Runnable实现类对象作为实参传递。 Thread thread = new Thread(d,"狗娃"); //Thread类使用Target变量记录了d对象, //调用thread对象的start方法开启线程。 thread.start(); for(int i = 0 ; i < 100 ; i++){ System.out.println(Thread.currentThread().getName()+":"+i); } } /* Thread类 的run方法 * @Override public void run() { if (target != null) { target.run(); //就相当于Runnable实现类的对象的run方法作为了Thread对象的任务代码了。 } } */ }
方式三:实现Callable接口,回调获取线程结果
package thread; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class TestCallable implements Callable<Integer> { @Override public Integer call() throws Exception { int i = 0; for (; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } return i; } public static void main(String[] args) { TestCallable tCallable = new TestCallable(); FutureTask<Integer> fTask = new FutureTask<Integer>(tCallable); new Thread(fTask, "子线程").start(); for (int i = 0; i <100000; i++) { System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i); } } }