目录
3.在Main方法中,把学生类对象作为Thread对象的构造函数参数传入,并启动
3.创建FutureTask,传入实现Callable的学生类对象作为参数
一、继承Thread类,重写run方法
示例:
1.准备一个普通的学生类
学生类属性有学生的名字、学生跳的次数。
方法有
构造方法:初始化学生名字。
跳行为方法:执行该方法一次、学生跳的次数就加1,并输出语句。
public class StudentThread {
private String name;
private int jump;
public StudentThread(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
}
2.学生类继承Thread接口,并重写run方法
在run方法中,我们让跳方法执行20次,也代表线程开启后会执行20次。
public class StudentThread extends Thread {
private String name;
private int jump;
public StudentThread(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
@Override
public void run() {
for (int i = 0;i<20;i++)
{
this.Jump();
}
}
}
3.在Main方法中生成两个学生线程,并启动
public class Main {
public static void main(String[] args) {
StudentThread sh1 = new StudentThread("冷少");
StudentThread sh2 = new StudentThread("小松");
sh1.start();
sh2.start();
}
}
4.输出结果
“小松” 与 “冷少” 交替输出 证明创建成功
二、实现Runnable接口
示例:
1.准备一个普通的学生类
学生类属性有学生的名字、学生跳的次数。
方法有
构造方法:初始化学生名字。
跳行为方法:执行该方法一次、学生跳的次数就加1,并输出语句。
public class StudentRunnable{
private String name;
private int jump;
public StudentRunnable(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
}
2.实现Runnable接口,重写run方法
与重写Thread的run方法类似
public class StudentRunnable implements Runnable {
private String name;
private int jump;
public StudentRunnable(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
@Override
public void run() {
for (int i = 0;i<20;i++)
{
this.Jump();
}
}
}
3.在Main方法中,把学生类对象作为Thread对象的构造函数参数传入,并启动
public class Main {
public static void main(String[] args) {
Thread thread1 = new Thread(new StudentRunnable("冷少"));
Thread thread2 = new Thread(new StudentRunnable("小松"));
thread1.start();
thread2.start();
}
}
4.输出结果
“小松” 与 “冷少” 交替输出 证明创建成功
三、实现Callable接口
示例:
1.准备一个普通的学生类
学生类属性有学生的名字、学生跳的次数。
方法有
构造方法:初始化学生名字。
跳行为方法:执行该方法一次、学生跳的次数就加1,并输出语句。
public class StudentCallable{
private String name;
private int jump;
public StudentCallable(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
}
2.实现Callable接口,重写call方法
1.Callable<T> T的返回值与call方法的返回值相同
2.Callable<T> T为Callable实现类FutureTask的get方法返回类型
public class StudentCallable implements Callable<String> {
private String name;
private int jump;
public StudentCallable(String name)
{
this.name = name;
}
private void Jump()
{
System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
}
@Override
public String call() {
for (int i = 0;i<20;i++)
{
this.Jump();
}
return this.name + "跳完了!";
}
}
3.创建FutureTask,传入实现Callable的学生类对象作为参数
FutureTask<T> T 与 实现Callable的对象类的call方法返回类型相同
public class Main {
public static void main(String[] args) {
FutureTask<String> ft1 = new FutureTask<>(new StudentCallable("冷少"));
FutureTask<String> ft2 = new FutureTask<>(new StudentCallable("小松"));
Thread thread1 = new Thread(ft1);
Thread thread2 = new Thread(ft2);
thread1.start();
thread2.start();
try {
String ft1_return = ft1.get();
String ft2_return = ft2.get();
System.out.println(ft1_return);
System.out.println(ft2_return);
}
catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
}
}
4.输出结果
“小松” 与 “冷少” 交替输出 证明创建成功
并且在最后输出了线程结束时的返回值
以上:为个人学习时的笔记,如果有错误,欢迎各位大神指出