实验十六 线程技术
实验时间 2018-12-8
理论部分:
并发
⚫ 线程的概念
⚫ 中断线程
⚫ 线程状态
⚫ 多线程调度
⚫ 线程同步
1.线程的概念
程序是一段静态的代码,它是应用程序执行的蓝本。
进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程。
操作系统为每个进程分配一段独立的内存空间和系统资源,包括:代码数据以及堆栈等资源。每一个进程的内部数据和状态都是完全独立的。
多任务操作系统中,进程切换对CPU资源消耗较大。
多线程的概念
‐多线程是进程执行过程中产生的多条执行线索。 ‐线程是比进程执行更小的单位。
‐线程不能独立存在,必须存在于进程中,同一进 程的各线程间共享进程空间的数据。
‐每个线程有它自身的产生、存在和消亡的过程, 是一个动态的概念。
‐多线程意味着一个程序的多行语句可以看上去几 乎在同一时间内同时运行。
‐线程创建、销毁和切换的负荷远小于进程,又称 为轻量级进程(lightweight process)。
Java实现多线程有两种途径:
‐创建Thread类的子类 ‐在程序中定义实现Runnable接口的类
2.中断线程
当线程的run方法执行方法体中最后一条语句后, 或者出现了在run方法中没有捕获的异常时,线程将终止,让出CPU使用权。
调用interrupt()方法也可终止线程
3.线程状态
⚫ 利用各线程的状态变换,可以控制各个线程轮流 使用CPU,体现多线程的并行性特征。
⚫ 线程有如下7种状态:
➢ New (新建)
➢ Runnable (可运行)
➢ Running(运行)
➢ Blocked (被阻塞)
➢ Waiting (等待)
➢ Timed waiting (计时等待)
➢ Terminated (被终止)
4.多线程调度
– Java提供一个线程调度器来监控程序启动后进入 可运行状态的所有线程。线程调度器按照线程的 优先级决定应调度哪些线程来执行。
– 处于可运行状态的线程首先进入就绪队列排队等 候处理器资源,同一时刻在就绪队列中的线程可 能有多个。Java的多线程系统会给每个线程自动 分配一个线程的优先级。
Java 的线程调度采用优先级策略:
➢ 优先级高的先执行,优先级低的后执行;
➢ 多线程系统会自动为每个线程分配一个优先级,缺省 时,继承其父类的优先级;
➢ 任务紧急的线程,其优先级较高;
➢ 同优先级的线程按“先进先出”的队列原则;
1、实验目的与要求
(1) 掌握线程概念;
(2) 掌握线程创建的两种技术;
(3) 理解和掌握线程的优先级属性及调度方法;
(4) 掌握线程同步的概念及实现技术;
2、实验内容和步骤
实验1:测试程序并进行代码注释。
测试程序1:
l 在elipse IDE中调试运行ThreadTest,结合程序运行结果理解程序;
l 掌握线程概念;
l 掌握用Thread的扩展类实现线程的方法;
l 利用Runnable接口改造程序,掌握用Runnable接口创建线程的方法。
class Lefthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("You are Students!"); try{ sleep(500); } catch(InterruptedException e) { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("I am a Teacher!"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left=new Lefthand(); right=new Righthand(); left.start(); right.start(); } } |
Runnable接口改造的程序:
package demo; class Lefthand implements Runnable { public void run() { for (int i = 0; i <= 5; i++) { System.out.println("You are Students!"); try { Thread.sleep(500); } catch (InterruptedException e) { System.out.println("Lefthand error."); } } } } class Righthand implements Runnable { public void run() { for (int i = 0; i <= 5; i++) { System.out.println("I am a Teacher!"); try { Thread.sleep(300); } catch (InterruptedException e) { System.out.println("Righthand error."); } } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { Runnable left1 = new Lefthand(); Runnable right1 = new Righthand() ; Thread left = new Thread(left1); Thread right = new Thread(right1); left.start(); right.start(); } }
运行结果均为:
测试程序2:
l 在Elipse环境下调试教材625页程序14-1、14-2 、14-3,结合程序运行结果理解程序;
l 在Elipse环境下调试教材631页程序14-4,结合程序运行结果理解程序;
l 对比两个程序,理解线程的概念和用途;
l 掌握线程创建的两种技术。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package bounce; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Shows an animated bouncing ball. * @version 1.34 2015-06-21 * @author Cay Horstmann */ public class Bounce { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package bounce; import java.awt.*; import java.util.*; import javax.swing.*; /** * The component that draws the balls. * @version 1.34 2012-01-26 * @author Cay Horstmann */ public class BallComponent extends JPanel { private static final int DEFAULT_WIDTH = 450; private static final int DEFAULT_HEIGHT = 350; private java.util.List<Ball> balls = new ArrayList<>(); /** * Add a ball to the component. * @param b the ball to add */ public void add(Ball b) { balls.add(b); } public void paintComponent(Graphics g) { super.paintComponent(g); // erase background Graphics2D g2 = (Graphics2D) g; for (Ball b : balls) { g2.fill(b.getShape()); } } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package bounce; import java.awt.geom.*; /** * A ball that moves and bounces off the edges of a rectangle * @version 1.33 2007-05-17 * @author Cay Horstmann */ public class Ball { private static final int XSIZE = 15; private static final int YSIZE = 15; private double x = 0; private double y = 0; private double dx = 1; private double dy = 1; /** * Moves the ball to the next position, reversing direction if it hits one of the edges */ public void move(Rectangle2D bounds) { x += dx; y += dy; if (x < bounds.getMinX()) { x = bounds.getMinX(); dx = -dx; } if (x + XSIZE >= bounds.getMaxX()) { x = bounds.getMaxX() - XSIZE; dx = -dx; } if (y < bounds.getMinY()) { y = bounds.getMinY(); dy = -dy; } if (y + YSIZE >= bounds.getMaxY()) { y = bounds.getMaxY() - YSIZE; dy = -dy; } } /** * Gets the shape of the ball at its current position. */ public Ellipse2D getShape() { return new Ellipse2D.Double(x, y, XSIZE, YSIZE); } }
运行结果:
测试程序3:
分析以下程序运行结果并理解程序。
class Race extends Thread { public static void main(String args[]) { Race[] runner=new Race[4]; for(int i=0;i<4;i++) runner[i]=new Race( ); for(int i=0;i<4;i++) runner[i].start( ); runner[1].setPriority(MIN_PRIORITY); runner[3].setPriority(MAX_PRIORITY);} public void run( ) { for(int i=0; i<1000000; i++); System.out.println(getName()+"线程的优先级是"+getPriority()+"已计算完毕!"); } } |
运行结果:
测试程序4
l 教材642页程序模拟一个有若干账户的银行,随机地生成在这些账户之间转移钱款的交易。每一个账户有一个线程。在每一笔交易中,会从线程所服务的账户中随机转移一定数目的钱款到另一个随机账户。
l 在Elipse环境下调试教材642页程序14-5、14-6,结合程序运行结果理解程序;
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package synch; /** * This program shows how multiple threads can safely access a data structure. * @version 1.31 2015-06-21 * @author Cay Horstmann */ public class SynchBankTest { public static final int NACCOUNTS = 100; public static final double INITIAL_BALANCE = 1000; public static final double MAX_AMOUNT = 1000; public static final int DELAY = 10; public static void main(String[] args) { Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE); for (int i = 0; i < NACCOUNTS; i++) { int fromAccount = i; Runnable r = () -> { try { while (true) { int toAccount = (int) (bank.size() * Math.random()); double amount = MAX_AMOUNT * Math.random(); bank.transfer(fromAccount, toAccount, amount); Thread.sleep((int) (DELAY * Math.random())); } } catch (InterruptedException e) { } }; Thread t = new Thread(r); t.start(); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package synch; import java.util.*; import java.util.concurrent.locks.*; /** * A bank with a number of bank accounts that uses locks for serializing access. * @version 1.30 2004-08-01 * @author Cay Horstmann */ public class Bank { private final double[] accounts; private Lock bankLock; private Condition sufficientFunds; /** * Constructs the bank. * @param n the number of accounts * @param initialBalance the initial balance for each account */ public Bank(int n, double initialBalance) { accounts = new double[n]; Arrays.fill(accounts, initialBalance); bankLock = new ReentrantLock(); sufficientFunds = bankLock.newCondition(); } /** * Transfers money from one account to another. * @param from the account to transfer from * @param to the account to transfer to * @param amount the amount to transfer */ public void transfer(int from, int to, double amount) throws InterruptedException { bankLock.lock(); try { while (accounts[from] < amount) sufficientFunds.await(); System.out.print(Thread.currentThread()); accounts[from] -= amount; System.out.printf(" %10.2f from %d to %d", amount, from, to); accounts[to] += amount; System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); sufficientFunds.signalAll(); } finally { bankLock.unlock(); } } /** * Gets the sum of all account balances. * @return the total balance */ public double getTotalBalance() { bankLock.lock(); try { double sum = 0; for (double a : accounts) sum += a; return sum; } finally { bankLock.unlock(); } } /** * Gets the number of accounts in the bank. * @return the number of accounts */ public int size() { return accounts.length; } }
运行结果:
一直模拟存取操作
综合编程练习
编程练习1
- 设计一个用户信息采集程序,要求如下:
(1) 用户信息输入界面如下图所示:
(2) 用户点击提交按钮时,用户输入信息显示控制台界面;
(3) 用户点击重置按钮后,清空用户已输入信息;
(4) 点击窗口关闭,程序退出。
代码:
package demo; import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { EventQueue.invokeLater(() -> { Frame frame = new Frame(); frame.setTitle("Student Detail"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); }); } }
package demo; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Frame extends JFrame { private JPanel panel; private JPanel panel1; private JPanel panel2; private JPanel buttonPanel; private JComboBox<String> faceCombo; private JCheckBox Reading; private JCheckBox Singing; private JCheckBox Dancing; private JPanel panelDanXuan; private ButtonGroup option; private JRadioButton optionA; private JRadioButton optionB; private static final int DEFAULT_WITH = 800; private static final int DEFAULT_HEIGHT = 400; public Frame() { //框架a panel = new JPanel(); panel.setPreferredSize(new Dimension(200,160)); panel.setLayout(new GridLayout(2,4)); JLabel lab = new JLabel("Name:", JLabel.CENTER); final JTextField jt = new JTextField(); JLabel lab1 = new JLabel("Qualification:", JLabel.CENTER); faceCombo = new JComboBox<>(); faceCombo.addItem("Graduate"); faceCombo.addItem("Not graduated"); JLabel lab2 = new JLabel("Adress:", JLabel.CENTER); final JTextArea jt1 = new JTextArea(); JLabel lab3 = new JLabel("Hobby:", JLabel.CENTER); panel1 = new JPanel(); Reading = new JCheckBox("Reading"); Singing = new JCheckBox("Singing"); Dancing = new JCheckBox("Dancing "); //框架b panel2 = new JPanel(); panel2.setPreferredSize(new Dimension(200,160)); JLabel lab4 = new JLabel("Sex:", JLabel.CENTER); panelDanXuan = new JPanel(); option = new ButtonGroup(); optionA = new JRadioButton("Male"); optionB = new JRadioButton("Female"); //框架c buttonPanel = new JPanel(); buttonPanel.setPreferredSize(new Dimension(200,80)); JButton jButton1 = new JButton("Validate"); JButton jButton2 = new JButton("Reset"); panel.add(lab); panel.add(jt); panel.add(lab1); panel.add(faceCombo); panel.add(lab2); panel.add(jt1); panel.add(lab3); panel1.add(Reading); panel1.add(Singing); panel1.add(Dancing); panel1.setBorder(BorderFactory.createTitledBorder("")); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); panel.add(panel1); panel2.add(lab4); option.add(optionA); option.add(optionB); panelDanXuan.add(optionA); panelDanXuan.add(optionB); panelDanXuan.setBorder(BorderFactory.createTitledBorder("")); panelDanXuan.setLayout(new BoxLayout(panelDanXuan, BoxLayout.Y_AXIS)); panel2.add(panelDanXuan); buttonPanel.add(jButton1); buttonPanel.add(jButton2); add(panel, BorderLayout.NORTH); add(panel2, BorderLayout.WEST); add(buttonPanel, BorderLayout.SOUTH); setSize(DEFAULT_WITH, DEFAULT_HEIGHT); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Name = jt.getText(); if (Name != null) { System.out.println("Name:"+Name); } String m = faceCombo.getSelectedItem().toString(); System.out.println("Qualification:"+m); String Adress = jt1.getText(); if (Adress != null) { System.out.println("Adress:"+Adress); } System.out.println("Hobby:"); if(Reading.isSelected()) { System.out.println(Reading.getText()); } if(Singing.isSelected()) { System.out.println(Singing.getText()); } if(Dancing.isSelected()) { System.out.println(Dancing.getText()); } System.out.println("Sex:"); if(optionA.isSelected()) { System.out.println(optionA.getText()); } if(optionB.isSelected()) { System.out.println(optionB.getText()); } } }); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt.setText(""); jt1.setText(""); faceCombo.setSelectedItem("Graduate"); Reading.setSelected(false); Singing.setSelected(false); Dancing.setSelected(false); option.clearSelection(); } }); } }
运行结果:
输入信息:
控制台输出:
点击重置:
2.创建两个线程,每个线程按顺序输出5次“你好”,每个“你好”要标明来自哪个线程及其顺序号。
代码:
package demo; class Lefthand extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(i+":a.你好!"); try { sleep(300); } catch (InterruptedException e) { System.out.println("Lefthand error."); } } } } class Righthand extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(i+":b.你好!"); try { sleep(300); } catch (InterruptedException e) { System.out.println("Righthand error."); } } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left = new Lefthand(); right = new Righthand(); left.start(); right.start(); } }
运行结果:
3. 完善实验十五 GUI综合编程练习程序。
1.身份证(删除了多余的代码)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package demo1; import java.awt.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new Main(); frame.setTitle("身份证"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package demo1; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Main extends JFrame { private static ArrayList<Student> studentlist; private static ArrayList<Student> list; private JPanel panel; private JPanel buttonPanel; private static final int DEFAULT_WITH = 600; private static final int DEFAULT_HEIGHT = 300; public Main() { studentlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("C:\\Users\\ASUS\\Desktop\\身份证号.txt"); try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String number = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String province = linescanner.nextLine(); Student student = new Student(); student.setName(name); student.setnumber(number); student.setsex(sex); int a = Integer.parseInt(age); student.setage(a); student.setprovince(province); studentlist.add(student); } } catch (FileNotFoundException e) { System.out.println("学生信息文件找不到"); e.printStackTrace(); } catch (IOException e) { System.out.println("学生信息文件读取错误"); e.printStackTrace(); } panel = new JPanel(); panel.setLayout(new BorderLayout()); JTextArea jt = new JTextArea(); panel.add(jt); add(panel, BorderLayout.NORTH); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(1, 7)); JButton jButton = new JButton("字典排序"); JButton jButton1 = new JButton("年龄最大和年龄最小"); JLabel lab = new JLabel("猜猜你的老乡"); JTextField jt1 = new JTextField(); JLabel lab1 = new JLabel("找找同龄人(年龄相近):"); JTextField jt2 = new JTextField(); JLabel lab2 = new JLabel("输入你的身份证号码:"); JTextField jt3 = new JTextField(); JButton jButton2 = new JButton("退出"); jButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Collections.sort(studentlist); jt.setText(studentlist.toString()); } }); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int max = 0, min = 100; int j, k1 = 0, k2 = 0; for (int i = 1; i < studentlist.size(); i++) { j = studentlist.get(i).getage(); if (j > max) { max = j; k1 = i; } if (j < min) { min = j; k2 = i; } } jt.setText("年龄最大:" + studentlist.get(k1) + "年龄最小:" + studentlist.get(k2)); } }); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); System.exit(0); } }); jt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String find = jt1.getText(); String text = ""; String place = find.substring(0, 3); for (int i = 0; i < studentlist.size(); i++) { if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) { text += "\n" + studentlist.get(i); jt.setText("老乡:" + text); } } } }); jt2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String yourage = jt2.getText(); int a = Integer.parseInt(yourage); int near = agenear(a); int value = a - studentlist.get(near).getage(); jt.setText("年龄相近:" + studentlist.get(near)); } }); jt3.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { list = new ArrayList<>(); Collections.sort(studentlist); String key = jt3.getText(); for (int i = 1; i < studentlist.size(); i++) { if (studentlist.get(i).getnumber().contains(key)) { list.add(studentlist.get(i)); jt.setText("emmm!你可能是:\n" + list); } } } }); buttonPanel.add(jButton); buttonPanel.add(jButton1); buttonPanel.add(lab); buttonPanel.add(jt1); buttonPanel.add(lab1); buttonPanel.add(jt2); buttonPanel.add(lab2); buttonPanel.add(jt3); buttonPanel.add(jButton2); add(buttonPanel, BorderLayout.SOUTH); setSize(DEFAULT_WITH, DEFAULT_HEIGHT); } public static int agenear(int age) { int min = 53, value = 0, k = 0; for (int i = 0; i < studentlist.size(); i++) { value = studentlist.get(i).getage() - age; if (value < 0) value = -value; if (value < min) { min = value; k = i; } } return k; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package demo1; public class Student implements Comparable<Student> { private String name; private String number ; private String sex ; private int age; private String province; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getnumber() { return number; } public void setnumber(String number) { this.number = number; } public String getsex() { return sex ; } public void setsex(String sex ) { this.sex =sex ; } public int getage() { return age; } public void setage(int age) { // int a = Integer.parseInt(age); this.age= age; } public String getprovince() { return province; } public void setprovince(String province) { this.province=province ; } public int compareTo(Student o) { return this.name.compareTo(o.getName()); } public String toString() { return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; } }
2.四则运算
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package ui_test; public class Main { public static void main(String[] args) { MyExGUI lg = new MyExGUI(); //new MyExGUI(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package ui_test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import javax.swing.*; public class MyExGUI extends JFrame { ArrayList<String> user_zongti = new ArrayList<String>(); ArrayList<String> user_zonganswer = new ArrayList<String>(); ArrayList<String> user_answer = new ArrayList<String>(); ArrayList<String> true_answer = new ArrayList<String>(); ArrayList<String> jta_timu = new ArrayList<String>(); ArrayList<String> jta_zong = new ArrayList<String>(); ArrayList<Integer> user_fenshu = new ArrayList<Integer>(); JMenuBar jm; // 菜单条组件 JMenu menu;// 菜单 JMenuItem item1, item2;// 菜单项 JMenu build; // 二级菜单 JMenuItem file, project; TextArea answer_all = new TextArea(); TextField jta = new TextField(); TextField jta_answer = new TextField(); JLabel num_answer = new JLabel(); JLabel answer; JToolBar jtb;// 工具条 JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb_next; int answer_count; int answer_fenshu; public MyExGUI() { // 创建菜单 jm = new JMenuBar(); menu = new JMenu("文件(F)"); menu.setMnemonic('f'); // 助记符 build = new JMenu("新建"); file = new JMenuItem("文件"); project = new JMenuItem("答题"); item1 = new JMenuItem("保存(S)"); item2 = new JMenuItem("退出"); answer = new JLabel("第 1 题"); // 添加菜单项至菜单上 build.add(file); build.add(project); menu.add(build); menu.add(item1); menu.add(item2); menu.addSeparator(); // 将菜单加入至菜单栏 jm.add(menu); JPanel contentPanel = new JPanel(); contentPanel.setLayout(null); JLabel daan = new JLabel("答案"); JLabel dengyu = new JLabel("="); num_answer = answer; num_answer.setFont(new Font("宋体", Font.BOLD, 22)); jb_next = new JButton("下一题"); jta.setFont(new Font("宋体", Font.BOLD, 22)); jta_answer.setFont(new Font("宋体", Font.BOLD, 22)); jb_next.setFont(new Font("宋体", Font.BOLD, 22)); daan.setFont(new Font("宋体", Font.BOLD, 22)); dengyu.setFont(new Font("宋体", Font.BOLD, 22)); contentPanel.add(num_answer); contentPanel.add(daan); contentPanel.add(dengyu); contentPanel.add(jta); contentPanel.add(jta_answer); contentPanel.add(answer_all); contentPanel.add(jb_next); num_answer.setBounds(90, 20, 130, 50); daan.setBounds(250, 20, 90, 50); jta.setBounds(50, 70, 150, 30); dengyu.setBounds(205, 70, 20, 20); jta_answer.setBounds(230, 70, 100, 30); jb_next.setBounds(350, 70, 110, 30); answer_all.setBounds(50, 120, 400, 300); this.setJMenuBar(jm); // 添加菜单栏,不能设定位置,会自动放在最上部 this.add(contentPanel); this.setTitle("在线答题系统"); this.setSize(600, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { FileOutputStream outSTr = null; BufferedOutputStream Buff = null; boolean flag = true; File file; // String test ; do { // test = "test"+count; String inputValue = JOptionPane.showInputDialog("Please input file name"); file = new File(inputValue + "test.txt"); if (!file.exists()) { // 创建文件 try { flag = file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } flag = false; } else { JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE); flag = true; } } while (flag); // 写入文件 String u_answer; try { outSTr = new FileOutputStream(file); Buff = new BufferedOutputStream(outSTr); System.out.println("选择是后执行的代码" + user_zongti.size() + user_answer.size()); for (int i = 0; i < user_zongti.size(); i++) { try { Buff.write(user_zongti.get(i).getBytes()); Buff.write(" ".getBytes()); u_answer = user_answer.get(i); if (u_answer.equals("")) u_answer = "没有作答"; Buff.write(u_answer.getBytes()); Buff.write("\r\n".getBytes()); } catch (IOException e) { e.printStackTrace(); i--; } } Buff.flush(); Buff.close(); } catch (IOException e) { e.printStackTrace(); } try { outSTr.close(); } catch (IOException e) { e.printStackTrace(); } user_zongti.clear(); user_answer.clear(); } }); project.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { arithmetic art = new arithmetic(); true_answer = art.list_answer; jta_timu = art.list_timu; jta_zong = art.list; answer_count = 1; answer_all.setText(""); for (int i = 0; i < art.list_timu.size(); i++) { user_zongti.add(jta_zong.get(i)); answer_all.append(jta_timu.get(i)); answer_all.append("\r\n"); } num_answer.setText("第 " + answer_count + " 题"); jta.setText(jta_timu.get(answer_count - 1)); answer_count++; } }); jb_next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String temp; temp = jta_answer.getText(); if (jta.getText().equals("")) { JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE); return; } jta_answer.setText(""); if (answer_count <= 10) { if (isInteger(temp)) { user_answer.add(temp); System.out.println("选择否后执行的代码" + temp + "user_size" + user_answer.size()); num_answer.setText("第 " + answer_count + " 题"); jta.setText(jta_timu.get(answer_count - 1)); answer_count++; } else { JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE); } } else { user_answer.add(temp); System.out.println("选择否后执行的代码" + temp + "user_size" + user_answer.size()); answer_fenshu = 0; for (int i = 0; i < user_answer.size(); i++) { if (user_answer.get(i).equals(true_answer.get(i))) answer_fenshu += 5; } user_fenshu.add(answer_fenshu); Object[] options = { "是", "取消" }; int res = JOptionPane.showOptionDialog(null, "是否查看成绩", "答题完毕", JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, null, options, options[0]); if (res == JOptionPane.YES_OPTION) { chart ct = new chart(user_fenshu); ct.setVisible(true); } else { Object[] option = { "是", "取消" }; int res1 = JOptionPane.showOptionDialog(null, "是否退出程序", "终止框", JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, null, option, option[0]); if (res1 == JOptionPane.YES_OPTION) { dispose(); System.exit(0); } else { } } } } }); item2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); System.exit(0); } }); } public static boolean isInteger(String str) { for (int i = str.length(); --i >= 0;) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package ui_test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class arithmetic { ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list_timu = new ArrayList<String>(); ArrayList<String> list_answer = new ArrayList<String>(); public arithmetic() { FileOutputStream outSTr = null; BufferedOutputStream Buff = null; int number_n = 10, count; ArrayList<String> list_temp = new ArrayList<String>(); String[] operator = new String[] { "+", "-", "*", "/" }; Random rand = new Random(); File file1 = new File("test.txt"); if (file1.exists()) { // 创建文件 try { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } while (number_n > 0) { int[] number_temp = new int[rand.nextInt(2) + 3]; String[] str_temp = new String[number_temp.length - 1]; for (int i = 0; i < number_temp.length; i++) { if (i < number_temp.length - 1) { number_temp[i] = rand.nextInt(100); list_temp.add(String.valueOf(number_temp[i])); str_temp[i] = operator[rand.nextInt(4)]; list_temp.add(str_temp[i]); } else { number_temp[i] = rand.nextInt(100); list_temp.add(String.valueOf(number_temp[i])); } } count = calculate_RPN(produce_RPN(list_temp)); if (count != -1) { list_timu.add(transform_string(list_temp)); list_answer.add(String.valueOf(count)); list_temp.add(" = " + count); list.add(transform_string(list_temp)); number_n--; list_temp.clear(); } else list_temp.clear(); System.out.println(number_n); } try { outSTr = new FileOutputStream(file1); Buff = new BufferedOutputStream(outSTr); for (int i = 0; i < list.size(); i++) { try { Buff.write(list.get(i).getBytes()); Buff.write("\r\n".getBytes()); } catch (IOException e) { e.printStackTrace(); i--; } } Buff.flush(); Buff.close(); } catch (IOException e) { e.printStackTrace(); } // Buff.close(); try { outSTr.close(); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); System.out.println(); } System.out.print("计算完毕!"); } public static int calculate_RPN(ArrayList<String> list_temp) { int i = 0, t; double a = 0, b = 0; String l_temp; Stack sk = new Stack(10); for (t = 0; t < list_temp.size(); t++) { l_temp = list_temp.get(i++); if (!isInteger(l_temp)) { b = sk.mypop(); a = sk.mypop(); switch (l_temp) { case "+": sk.mypush(a + b); break; case "-": if(!(a<b)) { sk.mypush(a - b); } else return -1; break; case "*": sk.mypush(a * b); break; case "/": if (b == 0||a<b) return -1; sk.mypush(a / b); break; } System.out.println("st.mytop: " + sk.mypeek()); } else { sk.mypush((double) Integer.parseInt(l_temp)); } } if (!sk.myisempty()) { a = sk.mypop(); b = a - (int) a; System.out.println("a: " + a); if (a > 0 && b == 0) { return (int) a; } else return -1; } else return -1; } public static ArrayList<String> produce_RPN(ArrayList<String> list_temp) { int t = 0, i = 0; String tmp; Tack mytack = new Tack(10); ArrayList<String> lt_temp = new ArrayList<String>(); while (true) { tmp = list_temp.get(i++); if (isInteger(tmp)) { lt_temp.add(tmp); } else { if (mytack.myisempty()) { mytack.mypush(tmp); } else { if (isCPriority(tmp, mytack.mypeek())) mytack.mypush(tmp); else { lt_temp.add(mytack.mypop()); mytack.mypush(tmp); } } } if (i >= list_temp.size()) { while (!mytack.myisempty()) lt_temp.add(mytack.mypop()); System.out.println(transform_string(list_temp)); list_temp = lt_temp; System.out.println(list_temp); return list_temp; } } } public static boolean isInteger(String str) { for (int i = str.length(); --i >= 0;) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } public static boolean isCPriority(String str, String s) { if ((str + s).equals("*+") || (str + s).equals("*-") || (str + s).equals("/+") || (str + s).equals("/-")) return true; else return false; } public static String transform_string(ArrayList<String> list_temp) { String s = ""; for (int i = 0; i < list_temp.size(); i++) { s += list_temp.get(i); } return s; } static class Stack { int mytop; double stk[]; public Stack(int num) { mytop = -1; stk = new double[num]; } /* 出栈 */ double mypop() { double peek = stk[mytop]; mytop--; return peek; } /* 入栈 */ void mypush(double x) { mytop++; stk[mytop] = x; } /* 判空 */ Boolean myisempty() { if (mytop == -1) return true; else return false; } /* 取栈顶元素 */ double mypeek() { double peek = stk[mytop]; return peek; } /* 栈大小 */ int mysize() { return mytop + 1; } } static class Tack { int mytop; String tk[]; public Tack(int num) { mytop = -1; tk = new String[num]; } /* 出栈 */ String mypop() { String peek = tk[mytop]; mytop--; return peek; } /* 入栈 */ void mypush(String x) { mytop++; tk[mytop] = x; } /* 判空 */ Boolean myisempty() { if (mytop == -1) return true; else return false; } /* 取栈顶元素 */ String mypeek() { String peek = tk[mytop]; return peek; } /* 栈大小 */ int mysize() { return mytop + 1; } } }
学习感受:
1.线程终止的几种方法,有什么区别不太懂
2.多线程的优先级不太懂