目录
创建线程的方法:继承Thread类;实现Runnab类;匿名内部类;实现Callable类
/**继承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);
}
}
}
}
/**实现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);
}
}
}
}
/**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();
}
}
当 FutureTask 处于未启动或已启动状态时,执行 FutureTask.get()方法将导致调用线程阻塞。如果 FutureTask 处于已完成状态,调用FutureTask.get()方法将导致调用线程立即返回结果或者抛出异常。
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;
}
}
/**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 "线程进行中";
}
}
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();
}
}
/**匿名内部类*/
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();
}
}