02027_线程池练习:返回两个数相加的结果

1、要求:通过线程池中的线程对象,使用Callable接口完成两个数求和操作。

2、代码实现:

  (1)Callable接口实现类

 1 import java.util.concurrent.Callable;
 2
 3 public class MyCallable implements Callable<Integer> {
 4     // 成员变量
 5     int x = 5;
 6     int y = 3;
 7
 8     // 构造方法
 9     public MyCallable() {
10     }
11
12     public MyCallable(int x, int y) {
13         this.x = x;
14         this.y = y;
15     }
16
17     @Override
18     public Integer call() throws Exception {
19         return x + y;
20     }
21 }

  (2)测试类

 1 import java.util.concurrent.ExecutionException;
 2 import java.util.concurrent.ExecutorService;
 3 import java.util.concurrent.Executors;
 4 import java.util.concurrent.Future;
 5
 6 public class ThreadPoolDemo {
 7     public static void main(String[] args) throws InterruptedException,
 8             ExecutionException {
 9         // 创建线程池对象
10         ExecutorService threadPool = Executors.newFixedThreadPool(2);
11
12         // 创建一个Callable接口子类对象
13         // MyCallable c = new MyCallable();
14         MyCallable c = new MyCallable(100, 200);
15         MyCallable c2 = new MyCallable(10, 20);
16
17         // 获取线程池中的线程,调用Callable接口子类对象中的call()方法, 完成求和操作
18         // <Integer> Future<Integer> submit(Callable<Integer> task)
19         // Future 结果对象
20         Future<Integer> result = threadPool.submit(c);
21         // 此 Future 的 get 方法所返回的结果类型
22         Integer sum = result.get();
23         System.out.println("sum=" + sum);
24
25         // 再演示
26         result = threadPool.submit(c2);
27         sum = result.get();
28         System.out.println("sum=" + sum);
29         // 关闭线程池(可以不关闭)
30
31     }
32 }

3、运行结果:

  

  

l  Callable接口实现类

时间: 2024-08-02 20:31:41

02027_线程池练习:返回两个数相加的结果的相关文章

CUDA学习(三)之使用GPU进行两个数相加

在CPU上定义两个数并赋值,然后使用GPU核函数将两个数相加并返回到CPU,在CPU上显示 #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <iomanip> #include <iostream> #include <stdio.h> using namespace std; //检测GPU bool CheckCUDA(void)

两个数相加

给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 package com.leetcode.part1; /** * @aut

实现两个数相加不用四则运算

分析:实现两个是相加不用四则运算,根据计算机中的运算不用四则运算那么肯定是位运算了. (以下分析来自剑指offer)比如我们计算5+17=22这个结果,世界上,我们可以分为3个步骤计算,第一步各位数相加不进位,此时的结果是12(个位相加不进位是2,十位相加是1),所以结果是12: 第二步做进位,5+7有进位,进位值是10:第三步是把前两步计算结果加起来.12 + 10 = 22. 那么运用位运算二进制的数字也可以这么考虑,5是二进制的101,17是二进制的10001.试着把计算分为3步走,第一步

2-36进制的 两个数相加

最近在很多场合都看见设计模式的影子,一直以来,都投入主要时间在搞算法与数据结构,很来发现设计模式真的很重要.有的时候代码的可维护.可重用.可扩展确实胜过单纯的算法效率高.所以拾起大牛书籍<大话设计模式>同时参考网上诸大牛的博客,开始我的设计模式之旅.由于平时编程时用C/C++,现在是Java,也练练Java语法. 今天先介绍一下命令模式. 概念: 命令模式(Command):将一个请求封装成一个对象,从而使你可用不同的请求对象对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作.

leetcode 1: 找出两个数相加等于给定数 two sum

问题描述 对于一个给定的数组,找出2个数,它们满足2个数的和等于一个特定的数,返回这两个数的索引.(从1开始) Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target,

用链表实现两个数相加

说明:使用链表实现两个数的和,数的高位存储在链表的头部,最后输出结果.注:使用了翻转链表的功能. #include<stdio.h> #include<stdlib.h> struct Node { int value; Node *next; }; Node *reverseList(Node *head) { Node *pCur=head; Node *pPre=NULL; Node *rHead=NULL; while(pCur!=NULL) { Node *pNext=p

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

LeetCode Add Two Numbers 两个数相加

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *creatnode(int a){ 12 ListNode *nod=new ListNode(a);

10、java5线程池之返回结果的任务之Callable与Future

JDK文档描述Callable: public interface Callable<V>返回结果并且可能抛出异常的任务.实现者定义了一个不带任何参数的叫做 call 的方法. Callable 接口类似于 Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的.但是 Runnable 不会返回结果,并且无法抛出经过检查的异常. Executors 类包含一些从其他普通形式转换成 Callable 类的实用方法 唯一的一个方法是: call V call() throws Exc