【训练8】模式敲击键盘(循环读取文件输入流的内容;synchronized同步块;wait():进入等待状态;notifyAll():进入就绪状态;文件输入流FileInputStream:读取文件)

这篇文章介绍了如何使用Java编写一个模拟打字练习,通过FileInputStream读取文件字节,利用线程休眠控制读取速度,并使用synchronized关键字实现暂停和继续功能,展示了一个基础的多线程编程应用。
摘要由CSDN通过智能技术生成

【训练8】模式敲击键盘
使用IO流按字节读取文件,并通过线程的休眠控制读取字节的速度,再将读取的字节显示在文本域中,最后使用synchronized关键字实现暂停读取和继续读取的功能。

/*【训练8】模式敲击键盘
 * 使用IO流按字节读取文件,并通过线程的休眠控制读取字节的速度,再将读取的
 * 字节显示在文本域中,最后使用synchronized关键字实现暂停读取和继续
 * 读取的功能。
 * */

package dxc;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;


public class Xl8 extends JFrame{
	
	private JPanel contentPane;
	private JTextArea textArea;
	private JButton btnStart;
	private ReadData readData;
	
	
	public Xl8() {
		init();
		addAction();
	}
	
	public void addAction() {
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String buttonName = btnStart.getText();
				if(buttonName.equals("开始打字")) {
					readData.start();
					btnStart.setText("敲累了,歇一会……");
				}else if(buttonName.equals("敲累了,歇一会……")) {
					readData.pause();
					btnStart.setText("革命尚未成功,同志仍需努力……");
				}else if(buttonName.equals("革命尚未成功,同志仍需努力……")) {
					readData.reStart();
					btnStart.setText("敲累了,歇一会……");
				}
			}
		});
	}
	
	public void init() {
		setResizable(false);
		setTitle("模拟英文打字");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		//内容面板
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5,5,5,5));
		contentPane.setLayout(new BorderLayout(0,0));
		contentPane.setBackground(Color.WHITE);
		//按钮面板
		JPanel buttonPane = new JPanel();
		contentPane.add(buttonPane,BorderLayout.SOUTH);
		buttonPane.setBackground(Color.WHITE);
		//开始打字按钮
		btnStart = new JButton("开始打字");
		buttonPane.add(btnStart);
		btnStart.setFont(new Font("微软雅黑",Font.PLAIN,16));
		//滚动面板
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane,BorderLayout.CENTER);
		//文本域
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		//
		readData = new ReadData(textArea);
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Xl8 frame = new Xl8();
		frame.setVisible(true);
	}

}

class ReadData2 extends Thread{
	
	private JTextArea textArea;
	static boolean flag = false;
	
	public ReadData2(JTextArea textArea) {
		this.textArea = textArea;
	}
	
	//挂起线程
	synchronized void pause() {
		flag = true;
	}
	
	//唤醒所有等待中的线程
	synchronized void reStart() {
		notifyAll();
		flag = false;
	}
	
	@Override
	public void run() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("E:\\BaiduNetdiskDownload\\2. TM(示例源码+习题答案)\\TM(示例源码+习题答案)\\sl\\20\\编程训练\\8\\白雪公主.txt");
			byte[] b = new byte[1];	//可容纳一个字节的缓冲区
			StringBuilder sb = new StringBuilder();
			
			while(fis.read(b) != -1) {	//循环读取文件输入流,循环读入缓冲区中。
				String str = new String(b);//循环将字节内容,转换为字符串内容
				sb.append(str);
				
				synchronized(this){
					while(flag) {
						try {
							wait();
						}catch(InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				
				textArea.setText(sb.toString());
				
				try {
					Thread.sleep(200);
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				
			}
			
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fis.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值