Java实现新建有返回值的线程的示例详解

目录

一、题目描述

二、解题思路

三、代码详解

一、题目描述

题目:使用ThreadLocal管理一号和二号线程,分别存入100元,在三号线程中使用利用一号和二号的计算结果来算出账户的实际金额。

二、解题思路

创建一个类:SynchronizedBankFrame,继承JFrame类

写一个内部类Bank

定义一个account变量,来表示账户。

deposit():一个存钱的方法

getAccount():显示账户余额的方法。

写一个内部类Transfer,实现Callable接口

在重写call()方法时,将返回值设置成账户的余额。

编写do_button_actionPerformed()方法,用来监听单击“开始存钱”按钮事件。在该方法中,分别获得了两个ThreadLocal变量的结果并计算最后的存钱结果。

Callabler接口类似于Runnable,区别在于Callabler会返回结果。

Future接口表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果。计算完成后,只能使用get()方法来获取结果。如果有必要,可以在计算完成前阻塞此方法。

三、代码详解

SynchronizedBankFrame

package com.xiaoxuzhu; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import java.awt.Font; import javax.swing.UIManager; /** * Description: * * @author xiaoxuzhu * @version 1.0 * * <pre> * 修改记录: * 修改后版本 修改人修改日期修改内容 * 2022/5/14.1 xiaoxuzhu2022/5/14 Create * </pre> * @date 2022/5/14 */ public class SynchronizedBankFrame extends JFrame { /** * */ private static final long serialVersionUID = 2671056183299397274L; private JPanel contentPane; private JTextArea thread1TextArea; private JTextArea thread2TextArea; private JTextArea thread3TextArea; /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { SynchronizedBankFrame frame = new SynchronizedBankFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SynchronizedBankFrame() { setTitle("新建有返回值的线程"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel, BorderLayout.SOUTH); JButton startButton = new JButton("开始存钱"); startButton.setFont(new Font("微软雅黑", Font.PLAIN, 16)); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_button_actionPerformed(arg0); } }); buttonPanel.add(startButton); JPanel processPanel = new JPanel(); contentPane.add(processPanel, BorderLayout.CENTER); processPanel.setLayout(new GridLayout(0, 3, 5, 5)); JPanel thread1Panel = new JPanel(); processPanel.add(thread1Panel); thread1Panel.setLayout(new BorderLayout(0, 0)); JLabel thread1Label = new JLabel("一号线程"); thread1Label.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread1Label.setHorizontalAlignment(SwingConstants.CENTER); thread1Panel.add(thread1Label, BorderLayout.NORTH); JScrollPane thread1ScrollPane = new JScrollPane(); thread1Panel.add(thread1ScrollPane, BorderLayout.CENTER); thread1TextArea = new JTextArea(); thread1TextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread1ScrollPane.setViewportView(thread1TextArea); JPanel thread2Panel = new JPanel(); processPanel.add(thread2Panel); thread2Panel.setLayout(new BorderLayout(0, 0)); JLabel thread2Label = new JLabel("二号线程"); thread2Label.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread2Label.setHorizontalAlignment(SwingConstants.CENTER); thread2Panel.add(thread2Label, BorderLayout.NORTH); JScrollPane thread2ScrollPane = new JScrollPane(); thread2Panel.add(thread2ScrollPane, BorderLayout.CENTER); thread2TextArea = new JTextArea(); thread2TextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread2ScrollPane.setViewportView(thread2TextArea); JPanel thread3Panel = new JPanel(); processPanel.add(thread3Panel); thread3Panel.setLayout(new BorderLayout(0, 0)); JLabel thread3Label = new JLabel("三号线程"); thread3Label.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread3Label.setHorizontalAlignment(SwingConstants.CENTER); thread3Panel.add(thread3Label, BorderLayout.NORTH); JScrollPane thread3ScrollPane = new JScrollPane(); thread3Panel.add(thread3ScrollPane, BorderLayout.CENTER); thread3TextArea = new JTextArea(); thread3TextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16)); thread3ScrollPane.setViewportView(thread3TextArea); } protected void do_button_actionPerformed(ActionEvent arg0) { Bank bank = new Bank(); Transfer transfer1 = new Transfer(bank, thread1TextArea);// 创建Transfer对象 Transfer transfer2 = new Transfer(bank, thread2TextArea);// 创建Transfer对象 FutureTask<Integer> task1 = new FutureTask<Integer>(transfer1);// 创建FutureTask对象 FutureTask<Integer> task2 = new FutureTask<Integer>(transfer2);// 创建FutureTask对象 Thread thread1 = new Thread(task1);// 创建一号线程 Thread thread2 = new Thread(task2);// 创建二号线程 thread1.start();// 运行一号线程 thread2.start();// 运行二号线程 try { int thread1Result = task1.get();// 获得一号线程的计算结果 int thread2Result = task2.get();// 获得二号线程的计算结果 thread3TextArea.setText(thread3TextArea.getText() + "一号计算结果是:" + thread1Result + "\n");// 更新三号线程文本域信息 thread3TextArea.setText(thread3TextArea.getText() + "二号计算结果是:" + thread2Result + "\n");// 更新三号线程文本域信息 thread3TextArea.setText(thread3TextArea.getText() + "实际的金额是:" + (thread1Result + thread2Result - 100) + "\n");// 更新三号线程文本域信息 } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }

Bank

public class Bank { private static ThreadLocal<Integer> account = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return 100; } }; public void deposit(int money) { account.set(account.get() + money); } public int getAccount() { return account.get(); } }

Transfer

package com.xiaoxuzhu; import java.util.concurrent.Callable; import javax.swing.JTextArea; /** * Description: * * @author xiaoxuzhu * @version 1.0 * * <pre> * 修改记录: * 修改后版本 修改人修改日期修改内容 * 2022/5/14.1 xiaoxuzhu2022/5/14 Create * </pre> * @date 2022/5/14 */ public class Transfer implements Callable<Integer> { private Bank bank; private JTextArea textArea; public Transfer(Bank bank, JTextArea textArea) {// 利用构造方法初始化变量 this.bank = bank; this.textArea = textArea; } public Integer call() { for (int i = 0; i < 10; i++) {// 循环10次向账户中存钱 bank.deposit(10); String text = textArea.getText(); textArea.setText(text + "账户的余额是:" + bank.getAccount() + "\n"); } return bank.getAccount();// 获得账户的余额 } }

以上就是Java实现新建有返回值的线程的示例详解的详细内容,更多关于Java新建有返回值线程的资料请关注易知道(ezd.cc)其它相关文章!

推荐阅读