热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java多线程实现:从1到100分段求和并汇总结果

本文介绍如何使用Java编写一个程序,通过10个线程分别计算不同区间的和,并最终汇总所有线程的结果。每个线程负责计算一段连续的整数之和,最后将所有线程的结果相加。

在编程中,多线程可以显著提高程序的并发性能。本文将详细讲解如何使用Java编写一个多线程程序,让每个线程负责计算一段连续整数的和,并最终汇总所有线程的结果。


任务描述:创建10个线程,第一个线程从1加到10,第二个线程从11加到20,以此类推,直到第十个线程从91加到100,最后将这10个线程的结果相加。



多线程的基本概念


多线程是指一个程序同时运行多个线程,每个线程可以独立执行不同的任务。Java提供了两种创建线程的方式:



  1. 继承Thread类:通过继承Thread类并重写run()方法来定义线程的行为。

  2. 实现Runnable接口:通过实现Runnable接口中的run()方法来定义线程的行为,并将其传递给Thread类的构造函数。



实现方式一:继承Thread类


public class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i <10; i++) {
Add add = new Add(i * 10 + 1, i * 10 + 10);
add.start();
}
Thread.sleep(50);
System.out.println("总和: " + Add.sum);
}
}

class Add extends Thread {
static Object lock = new Object();
static int sum;
private int start;
private int end;

public Add(int start, int end) {
this.start = start;
this.end = end;
}

public void run() {
int tempSum = 0;
for (int i = start; i <= end; i++) {
tempSum += i;
}
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " --- " + tempSum);
sum += tempSum;
}
}
}


实现方式二:实现Runnable接口


public class Test {
public static void main(String[] args) {
ABCD abcd = new ABCD();
Thread[] threads = new Thread[10];
for (int i = 0; i <10; i++) {
threads[i] = new Thread(abcd);
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("总和: " + ABCD.getSum());
}
}

class ABCD implements Runnable {
private static Object lock = new Object();
private static int sum = 0;
private static int num = 1;

public void run() {
synchronized (lock) {
for (int i = 0; i <10; i++, num++) {
sum += num;
System.out.println(Thread.currentThread().getName() + ": " + num);
}
}
}

public static int getSum() {
return sum;
}
}


两种方式的比较


相同点:



  • 都需要重写run()方法来定义线程的行为。

  • 启动线程时都需要调用Thread类的start()方法。


不同点:



  • 继承Thread类只能单继承,而实现Runnable接口可以实现多个接口。

  • 实现Runnable接口更有利于资源共享,适用于需要共享资源的场景。


结论:在大多数情况下,实现Runnable接口的方式优于继承Thread类,特别是在需要共享资源或多接口实现的场景下。



小彩蛋:简易版本


public class Test {
public static int sum = 0;
public static Object LOCK = new Object();

class ThreadTest extends Thread {
private int start;
private int end;

public ThreadTest(int start, int end) {
this.start = start;
this.end = end;
}

public void run() {
synchronized (LOCK) {
for (int i = start; i <= end; i++) {
sum += i;
}
System.out.println(Thread.currentThread().getName() + " --- " + sum);
}
}
}

public static void main(String[] args) throws InterruptedException {
Test test = new Test();
ThreadTest[] threads = new ThreadTest[10];
for (int i = 0; i <10; i++) {
threads[i] = test.new ThreadTest(i * 10 + 1, i * 10 + 10);
threads[i].setName((i + 1) + "号");
threads[i].start();
}
for (ThreadTest thread : threads) {
thread.join();
}
System.out.println("总和: " + sum);
}
}


以上代码展示了如何通过多线程实现从1到100分段求和,并最终汇总结果的过程。希望对您有所帮助!


推荐阅读
author-avatar
l87653644
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有