十三、线程

一、线程简介

二、Thread类

 

package 线程;

public class Thread继承 {

	public static void main(String[] args) {
		Thread A=new ThreadsA();
		A.start();
		
		Thread B=new ThreadsB();
		B.start();
		// TODO 自动生成的方法存根

	}

}

class ThreadsA extends Thread{
	public void run() {
		for(int i=0;i<=10;i++) {
			System.out.print(i+",");
			try {
				Thread.sleep(1000);//休眠停顿一秒
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
}

class ThreadsB extends Thread{
	public void run() {
		for(char j='a';j<='m';j++) {
			System.out.print(j+",");
			try {
				Thread.sleep(1000);//休眠停顿一秒
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
}

 

三、实现Runable 接口

 package 线程;

import java.awt.Container;
import javax.swing.*;


public class SwingAndThread extends JFrame {
	int count = 0; // 图标横坐标

	public SwingAndThread() {
		setBounds(300, 200, 250, 100); // 绝对定位窗体大小与位置
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container container = getContentPane();// 主容器
		container.setLayout(null); // 使窗体不使用任何布局管理器

		Icon icon = new ImageIcon("E:\\图片\\雪花.gif"); // 图标对象
		JLabel jl = new JLabel(icon);// 显示图标的标签
		jl.setBounds(10, 10, 200, 50); // 设置标签的位置与大小
		Thread t = new Thread() { // 定义匿名线程对象
			public void run() {
				while (true) {
					jl.setBounds(count, 10, 200, 50); // 将标签的横坐标用变量表示
					try {
						Thread.sleep(500); // 使线程休眠500毫秒
					} catch (InterruptedException e) {
						System.out.print("图片不存在");
					}
					count += 4; // 使横坐标每次增加4
					if (count >= 200) {
						// 当图标到达标签的最右边时,使其回到标签最左边
						count = 10;
					}
				}
			}
		};
		t.start(); // 启动线程
		container.add(jl); // 将标签添加到容器中
		setVisible(true); // 使窗体可见
		// 设置窗体的关闭方式
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new SwingAndThread(); // 实例化一个SwingAndThread对象
	}
}

 四、线程的生命周期

 五、线程的使用

 

package 线程;


import java.awt.BorderLayout;
import javax.swing.*;

public class JoinTest extends JFrame {
	private Thread threadA; // 定义两个线程
	private Thread threadB;
	private JProgressBar progressBar = new JProgressBar(); // 定义两个进度条组件
	private JProgressBar progressBar2 = new JProgressBar();

	public static void main(String[] args) {
		JoinTest test = new JoinTest();
		test.setVisible(true);
	}

	public JoinTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 200, 200, 100);
		getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条设置在窗体最北面
		getContentPane().add(progressBar2, BorderLayout.SOUTH); // 将进度条设置在窗体最南面
		progressBar.setStringPainted(true); // 设置进度条显示数字字符
		progressBar2.setStringPainted(true);
		// 使用匿名内部类形式初始化Thread实例
		threadA = new Thread(new Runnable() {
			int count = 0;

			public void run() { // 重写run()方法
				while (true) {
					progressBar.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程A休眠100毫秒
						threadB.join(); // 使线程B调用join()方法
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		threadA.start(); // 启动线程A
		threadB = new Thread(new Runnable() {
			int count = 0;

			public void run() {
				while (true) {
					progressBar2.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程B休眠100毫秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (count == 100) // 当count变量增长为100时
						break; // 跳出循环
				}
			}
		});
		threadB.start(); // 启动线程B
	}
}

 

 

 

package 线程;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class InterruptedSwing extends JFrame {

	public static void main(String[] args) {
		init(new InterruptedSwing(), 100, 100);
	}

	public InterruptedSwing() {
		JProgressBar progressBar = new JProgressBar(); // 创建进度条
		getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条放置在窗体合适位置
		JButton button = new JButton("停止");
		getContentPane().add(button, BorderLayout.SOUTH);
		progressBar.setStringPainted(true); // 设置进度条上显示数字
		Thread t = new Thread(new Runnable() {
			int count = 0;

			public void run() {
				while (true) {
					progressBar.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程休眠100毫秒
					} catch (InterruptedException e) { // 捕捉InterruptedException异常
						System.out.println("当前线程序被中断");
						break;
					}
				}
			}
		});

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t.interrupt(); // 中断线程
			}
		});
		t.start(); // 启动线程
	}

	public static void init(JFrame frame, int width, int height) {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.setVisible(true);
	}
}

 

 

五、线程的优先级 

 

package 线程;


public class PriorityTest implements Runnable {
	String name;

	public PriorityTest(String name) {
		this.name = name;
	}

	@Override
	public void run() {
//		String tmp = "";
//		for (int i = 0; i < 50000; i++) {// 完成五万次字符串拼接
//			tmp += i;
//		}
		System.out.println(name + "线程完成任务");
	}

	public static void main(String[] args) {
		Thread a = new Thread(new PriorityTest("A"));
		a.setPriority(1);// A线程优先级最小
		Thread b = new Thread(new PriorityTest("B"));
		b.setPriority(3);
		Thread c = new Thread(new PriorityTest("C"));
		c.setPriority(7);
		Thread d = new Thread(new PriorityTest("D"));
		d.setPriority(10);// D线程优先级最大
		a.start();
		b.start();
		c.start();
		d.start();
		
	}
}

 

 

 

 六、线程的同步机制

 

 

package 线程;

public class SynchronizedTest implements Runnable{
	int num=10;

	@Override
	public void run() {
		while(true) {
			if(num>0) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"---票数"+ num--);
			}
		}
		// TODO 自动生成的方法存根
		
	}
	public static void main(String[] args) {
		SynchronizedTest t=new SynchronizedTest();
		
		Thread ta=new Thread(t,"线程一");
		Thread tb=new Thread(t,"线程二");
		Thread tc=new Thread(t,"线程三");
		Thread td=new Thread(t,"线程四");
		ta.start();
		tb.start();
		tc.start();
		td.start();       
	}

}

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郭同学忒骚了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值