Java创建线程执行任务的方法(一)
2023-12-29 19:29:58
目录
创建线程的方法:继承Thread类;实现Runnab类;匿名内部类;实现Callable类
1.继承Thread类
/**继承Thread类*/
public class ThreadDemo extends Thread{
public static void main(String[] args) {
ThreadDemo threadDemo = new ThreadDemo();
/**start() 启动当前线程;调用当前线程run()方法*/
threadDemo.start();
}
/**重写run()*/
@Override
public void run() {
/**执行run()中间的任务*/
int num =100;
for (int i=1;i<=num;i++){
if (i%2 ==0){
System.out.println("1-100的偶数为:" + i);
}
}
}
}
2.实现Runnab类
2.1实现Runnable类
/**实现Runnable类*/
public class ThreadDemo implements Runnable {
public static void main(String[] args) {
ThreadDemo threadDemo = new ThreadDemo();
/**获取Thread对象,将自定义的ThreadDemo对象存进去*/
Thread thread = new Thread(threadDemo);
/**start() 启动当前线程;调用当前线程run()方法*/
thread.start();
}
/**重写run()*/
@Override
public void run() {
/**执行run()中间的任务*/
int num =100;
for (int i=1;i<=num;i++){
if (i%2 ==0){
System.out.println("1-100的偶数为:" + i);
}
}
}
}
?2.2使用Lambda表达式
/**Lambda表达式*/
public class ThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
int num = 20;
for (int i = 1; i <= num; i++) {
if (i % 2 != 0) {
System.out.println("1-20的奇数为:" + i);
}
}
});
/**start() 启动当前线程;调用当前线程run()方法*/
thread.start();
}
}
/**Lambda表达式*/
public class ThreadDemo {
public static void main(String[] args) {
new Thread(() -> {
/**执行->箭头后面的任务*/
System.out.println("线程");
}).start();
}
}
3.实现Callable类
当 FutureTask 处于未启动或已启动状态时,执行 FutureTask.get()方法将导致调用线程阻塞。如果 FutureTask 处于已完成状态,调用FutureTask.get()方法将导致调用线程立即返回结果或者抛出异常。
3.1返回Integer类型数据
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**Callable方法*/
public class ThreadDemo implements Callable<Integer> {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> integerFutureTask = new FutureTask<>(new ThreadDemo());
Thread thread = new Thread(integerFutureTask);
thread.start();
Integer result = integerFutureTask.get();
System.out.println(result);
}
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 1; i <= 10; i++){
sum += i;
}
return sum;
}
}
3.2返回String类型数据
/**Callable方法*/
public class ThreadDemo implements Callable<String> {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> stringFutureTask = new FutureTask<>(new ThreadDemo());
Thread thread = new Thread(stringFutureTask);
thread.start();
/**输出 线程进行中 */
String result = stringFutureTask.get();
System.out.println(result);
/**输出 Thread-0 */
/*String name = thread.getName();*/
/*System.out.println(name);*/
}
@Override
public String call() throws Exception {
return "线程进行中";
}
}
3.3返回Object类型数据
public class User {
private String name = "张三";
private int age = 18;
private String gender ="男";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public User(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
/**Callable方法*/
public class ThreadDemo implements Callable<Object> {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Object> stringFutureTask = new FutureTask<>(new ThreadDemo());
Thread thread = new Thread(stringFutureTask);
thread.start();
Object result = stringFutureTask.get();
/**输出 User{name='张三', age=18, gender='男'}*/
System.out.println(result);
}
@Override
public Object call() throws Exception {
User user = new User();
/** 返回 包名.类名@哈希码值(demo.User@135fbaa4)*/
/*return user;*/
/**返回 User{name='张三', age=18, gender='男'}*/
return user.toString();
}
}
4.匿名内部类
/**匿名内部类*/
public class ThreadDemo {
public static void main(String[] args) {
/**获取Thread对象,使用匿名内部类方式获取Runnable对象*/
Thread thread = new Thread(new Runnable() {
/**重写run()*/
@Override
public void run() {
/**执行run()中间的任务*/
int num = 100;
for (int i = 1; i <= num; i++) {
if (i % 2 != 0) {
System.out.println("1-100的奇数为:" + i);
}
}
}
});
/**start() 启动当前线程;调用当前线程run()方法*/
thread.start();
}
}
文章来源:https://blog.csdn.net/qq_53376718/article/details/135296900
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!