更新时间:2021-06-04 15:44:55 来源:动力节点 浏览1097次
1.创建线程的三种方式
使用Thread
package com.wpbxx.test;
//1.自定义一个类,继承java.lang包下的Thread类
class MyThread extends Thread{
//2.重写run方法
@Override
public void run() {
//3.将要在线程中执行的代码编写在run方法中
for(int i = 0; i < 1000; i++) {
System.out.println("wpb");
}
}
}
public class helloworld {
public static void main(String[] args) {
//4.创建上面自定义类的对象
MyThread mt = new MyThread();
//5.调用start方法启动线程
mt.start();
for(int i = 0; i< 1000; i++) {
System.out.println("xx");
}
}
}
使用Runnable
package com.wpbxx.test;
//1.自定义一个类实现java.lang包下的Runnable接口
class MyRunnable implements Runnable{
//2.重写run方法
@Override
public void run() {
//3.将要在线程中执行的代码编写在run方法中
for(int i = 0; i < 1000; i++) {
System.out.println("wpb");
}
}
}
public class helloworld {
public static void main(String[] args) {
//4.创建上面自定义类的对象
MyRunnable mr = new MyRunnable();
//5.创建Thread对象并将上面自定义类的对象作为参数传递给Thread的构造方法
Thread t = new Thread(mr);
//6.调用start方法启动线程
t.start();
for(int i = 0; i < 1000; i++) {
System.out.println("xx");
}
}
}
使用Callable接口创建的线程会获得一个返回值并且可以声明异常。
优点: 可以获取返回值 可以抛出异常
线程池是初始化一个多线程应用程序过程中创建一个线程集合,然后在需要执行新的任务时直接去这个线程集合中获取,而不是创建一个线程。任务执行结束后,线程回到池子中等待下一次的分配。
线程池的作用
解决创建单个线程耗费时间和资源的问题。
package com.wpbxx.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
//1.自定义一个类实现java.util.concurrent包下的Callable接口
class MyCallable implements Callable<Integer>{
private int count;
public MyCallable(int count) {
this.count = count;
}
//2.重写call方法
@Override
public Integer call() throws Exception{
//3.将要在线程中执行的代码编写在call方法中
for(int i = 0; i < 100; i++) {
count++;
}
return count;
}
}
public class helloworld {
public static void main(String[] args) {
//4.创建ExecutorService线程池 里面为线程的数量
ExecutorService es = Executors.newFixedThreadPool(2);
////创建一个线程池,里面的线程会根据任务数量进行添加
//ExecutorService es = Executors.newCachedThreadPool();
//5.将自定义类的对象放入线程池里面
Future<Integer> f1= es.submit(new MyCallable(5));
Future<Integer> f2 = es.submit(new MyCallable(3));
try {
//6.获取线程的返回结果
System.out.println(f1.get());
System.out.println(f2.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//7.关闭线程池,不再接收新的线程,未执行完的线程不会被关闭
es.shutdown();
}
}
继承Thread
优点:可以直接使用Thread类中的方法,代码简单
缺点:继承Thread类之后就不能继承其他的类
实现Runnable接口
优点:即时自定义类已经有父类了也不受影响,因为可以实现多个接口
缺点:在run方法内部需要获取到当前线程的Thread对象后才能使用Thread中的方法
实现Callable接口
优点:可以获取返回值,可以抛出异常
缺点:代码编写较为复杂
package com.wpbxx.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
//简易写法 使用匿名内部类创建多线程
public class helloworld {
public static void main(String[] args) throws InterruptedException, ExecutionException {
new Thread() {
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println("wpb");
}
}
}.start();
new Thread(new Runnable() {
public void run() {
for(int i = 0; i< 1000; i++) {
System.out.println("xx");
}
}
}).start();
ExecutorService exec = Executors.newCachedThreadPool();
Future<Integer> result = exec.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception{
return 1024;
}
});
System.out.println(result.get());
}
}
以上就是动力节点小编介绍的"Java多线程的相关代码",希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。
0基础 0学费 15天面授
有基础 直达就业
业余时间 高薪转行
工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习