java 线程控制的小程序

1. 要求
  • 线程Count负责报数字
  • 线程Master负责监听键盘输入,一旦输入的是大写字母S,线程Count就退出报数字,线程Master也退出
2. 代码
  • 以下代码都没有包含包信息,复制代码请注意随机应变
  • MyPanel : 绘画板,是监听按键的载体
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MyPanel extends JPanel implements KeyListener{
	private int keyCode;

	public int getKeyCode(){
		return keyCode;
	}

	@Override
	public void paint(Graphics g){
		super.paint(g);
		g.setColor(Color.green);
		g.setFont(new Font("宋体", Font.BOLD, 20));
		g.drawString("我是监控器,点击S结束count线程",20,110);
	}

	@Override
	public void keyTyped(KeyEvent e){

	}

	@Override
	public void keyPressed(KeyEvent e){
		keyCode = e.getKeyCode();
	}

	@Override
	public void keyReleased(KeyEvent e){

	}
}
  • Count : 报数字的线程
public class Count extends Thread{
	private int count;
	private boolean loop = true;

	@Override
	public void run(){
		while(loop){
			System.out.println("Count = " + (++count));//从0开始数数
			try{
				Thread.sleep(5000);//每数一个数,休眠半秒
			}catch(InterruptedException e){
				System.out.println("被终止了呢!那就不要数数了!");
			}
		}
	}//end run

	public void setLoop(boolean loop){
		this.loop = loop;
	}
}//end count
  • Master : 监管按键的线程
public class Master extends Thread{
	private final JFrame frame = new JFrame();//画板的载体
	private final MyPanel panel = new MyPanel();//画板,承接键盘命令的容器
	private final Count thread;//要控制的线程,从外界传进来

	{//把画板放入框架中
		frame.add(panel);
		frame.addKeyListener(panel);
		frame.setSize(400,400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public Master(Count thread){
		this.thread = thread;
	}

	@Override
	public void run(){
		while(true){
			int key = panel.getKeyCode();
			try{
				Thread.sleep(1);//为什么这里必须休眠一下才可以实现呢?连一毫秒也可以
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			if(key == KeyEvent.VK_S){
				thread.setLoop(false);
				if(thread.getState() == State.TIMED_WAITING){
					thread.interrupt();//由于count进程会休眠,有可能按下键的时候它在休眠,无法快速反应
				}
				break;//退出本线程
			}
		}
	}
}
  • Test : 测试代码
public class Test{
	public static void main(String[] args) throws InterruptedException{
		Count count = new Count();
		Master master = new Master(count);

		count.start();
		master.start();

	}
}
  • 以上可以达到目标
3. 疑问
  • 在Master线程的run方法中,不知道为什么一定要有一个缓冲的代码才能实现功能,如果直接判断if(key == KeyEvent.VK_S){},根本进不去,也不不知道为什么,百思不得其解,希望有大佬指点一下!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java多线程实现文件复制的示例程序: ```java import java.io.*; public class FileCopyThread extends Thread { private File sourceFile; private File targetFile; public FileCopyThread(File sourceFile, File targetFile) { this.sourceFile = sourceFile; this.targetFile = targetFile; } public void run() { try { FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(targetFile); byte[] buffer = new byte[4096]; // 缓冲区大小为4KB int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { File source = new File("source.txt"); File target = new File("target.txt"); int threadCount = 4; // 创建4个线程 long fileSize = source.length(); long blockSize = fileSize / threadCount; for (int i = 0; i < threadCount; i++) { long start = i * blockSize; long end = (i == threadCount - 1) ? fileSize : (i + 1) * blockSize; new FileCopyThread(source, target, start, end).start(); } } } ``` 该程序使用了多线程来复制文件。首先,将要复制的文件分成若干个块,每个块由一个线程处理。在 `FileCopyThread` 类的构造函数中,需要传入源文件和目标文件,以及该线程需要复制的文件块的起始和结束位置。在 `run()` 方法中,使用 `FileInputStream` 和 `FileOutputStream` 分别读取和写入文件,每次读取缓冲区大小的数据,并使用 `while` 循环重复读取和写入,直到文件复制完成。 在 `main()` 方法中,首先指定源文件和目标文件,然后指定要创建的线程数。接着,计算每个线程需要复制的文件块的起始和结束位置,并创建 `FileCopyThread` 对象,并启动该线程。 需要注意的是,该示例程序没有考虑多线程并发读写文件的问题,可能会产生竞争条件和文件损坏等问题。在实际应用中,需要使用线程同步机制来保证线程安全。同时,该程序还没有处理文件不存在等异常情况,也需要根据实际需求进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值