Java之线程编程的4种方法实现案例讲解

作者:一只可爱的小狐狸 时间:2021-11-18 18:40:46 

1、继承Thread


public class T4 {
public static void main(String[] args) {
System.out.println(Thread.currentThread());
Thread t1 = new A1();
t1.start();
}
}
class A1 extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++) {
System.out.println("Thread:"+Thread.currentThread().getName());
}
}
}

2、实现Runnable接口


public class T3 {
public static void main(String[] args) {
System.out.println("Thread:"+Thread.currentThread().getName());
Thread t1 = new Thread(new A2());
t1.start();
}

}
class A2 implements Runnable{
@Override
public void run() {
int res =0;
for(int i=0;i<10;i++) {
res+=i;
System.out.println("Thread:"+Thread.currentThread().getName());
}
}
}

3、使用Callable和Future接口创建线程


import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class T2 {
public static void main(String[] args) throws Exception {
System.out.println("Test3:" + Thread.currentThread().getName());
Callable c = new A4();
FutureTask ft = new FutureTask(c);
Thread t1 = new Thread(ft);
t1.start();
Object res = ft.get();
System.out.println("结果:" + res);
}
}
class A4 implements Callable {
@Override
public Object call() throws Exception {
int res = 0;
for (int i = 0; i < 10; i++) {
res += i;
System.out.println("Thread:" + Thread.currentThread().getName());
}
return res;
}
}

4、使用线程池创建线程

享元模式
享元模式Flyweight Pattern主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于 结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式
优点:大大减少对象的创建,降低系统内存的使用,以提高程序的执行效率。
缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部 状态的变化而变化,否则会造成系统的混乱。


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class T1 {
public static void main(String[] args) throws Exception {
Future[] arr = new Future[5];
ExecutorService es = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
arr[i] = es.submit(new A4());
}
for (Future f : arr) {
Object res = f.get();
System.out.println("结果为:" + res);
}
es.shutdown();
}
}

class A4 implements Callable {
@Override
public Object call() throws Exception {
int res = 0;
for (int i = 0; i < 10; i++) {
res += i;
System.out.println("Thread" + Thread.currentThread().getName());
}
return res;
}
}

来源:https://blog.csdn.net/qq_45874107/article/details/113754912

标签:Java,线程
0
投稿

猜你喜欢

  • java使用OGEngine开发2048

    2023-07-23 06:07:43
  • Go Java算法猜数字游戏示例详解

    2022-03-02 21:01:50
  • Java实现医院管理系统

    2023-11-22 18:36:07
  • 如何实现Spring Event(异步事件)

    2023-08-23 05:06:47
  • MyBatis-Plus中最简单的查询操作教程(Lambda)

    2022-03-16 13:43:28
  • 一文看懂JAVA设计模式之工厂模式

    2023-11-27 02:30:54
  • WPF实现调用本机摄像头的示例代码

    2023-03-15 15:40:24
  • C#使用Object类实现栈的方法详解

    2021-08-03 17:36:16
  • Java实战之实现一个好用的MybatisPlus代码生成器

    2023-06-18 02:16:10
  • SpringBoot项目的配置文件中设置server.port不生效问题

    2022-11-13 06:01:26
  • 关于@Scheduled不执行的原因分析

    2021-06-27 17:13:20
  • java实现手写一个简单版的线程池

    2022-09-29 04:59:32
  • java中用String.Join美化代码的实例讲解

    2022-03-04 08:17:04
  • java split用法详解及实例代码

    2022-06-27 06:48:19
  • Java集合框架之List ArrayList LinkedList使用详解刨析

    2022-04-24 13:43:43
  • Java实现驼峰、下划线互转的方法

    2023-08-18 09:17:54
  • java中文传值乱码问题的解决方法

    2023-11-25 16:26:47
  • java实现163邮箱发送邮件到qq邮箱成功案例

    2023-09-18 02:38:09
  • Spring Boot整合Mybatis并完成CRUD操作的实现示例

    2023-11-09 04:36:46
  • Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer异常

    2022-07-03 11:12:34
  • asp之家 软件编程 m.aspxhome.com