(1)
package Test;
public class test {
public static void main(String[] args) {
MyThread one = new MyThread("线程一");
MyThread two = new MyThread("线程二");
MyThread three = new MyThread("线程三");
one.start();//线程启动
two.start();
three.start();
}
}
class MyThread extends Thread {
final String tag;//当前线程的名字
public MyThread(String tag) {
super();
this.tag = tag;
}
@Override
public void run() {
for (int i = 1; i <= 6; ++i) {
System.out.println(tag + "第" + i + "次运行");
if (i == 3) {
//第三次时睡眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
(2)
package Test;
public class test {
/*
* 启动比赛
*/
public static int LEN = 100;// 路线总长
public static int Sum = 2;// 参赛人数
public static void main(String[] args) {
Athlet rabbit = new Athlet(3, 10, "兔子");
Athlet trot = new Athlet(1, 3, "乌龟");
rabbit.start();
trot.start();
}
}
class Athlet extends Thread {
/*
* 运动员类
*/
final int V;// 速度
final int Sleep_Time;// 睡眠时间
final String Name;// 名字
public Athlet(int v, int sleep_Time, String name) {
V = v;
Sleep_Time = sleep_Time;
Name = name;
}
@Override
public void run() {
for (int i = 1; i <= test.LEN; i += V) {
System.out.println(Name + "跑了" + i + "米");
if ((i < test.LEN && i + V > test.LEN) || i == test.LEN / 2) {
// 跑到一半睡觉
try {
Thread.sleep(Sleep_Time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (Thread.activeCount() == test.Sum + 1) {
// 如果还在运行的线程数等于总人数+1(main方法的线程)即该对象第一个到达终点
System.out.println(Name + "胜利!");
}
}
}
(4)
package Test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class test extends JFrame {
/**
* 继承容器JFram创建窗口
*/
public static volatile boolean stop = false;// 线程是否停止//volatile修饰的成员变量可以在多线程之间达到共享
Button[] bt;
Thread[] th;
public test() {
this.setLayout(new FlowLayout());// 布局器
Container container = getContentPane();// 设置容器
// 添加按钮
bt = new Button[6];
th = new Thread[6];
for (int i = 0; i < bt.length; i++) {
bt[i] = new Button(Integer.toString((int) (Math.random() * 10)));
container.add(bt[i]);// 添加按钮
th[i] = new Thread(bt[i]);// 通过Thread类运行线程
}
JButton jb = new JButton("停止");
jb.setBorderPainted(false);// 边框不可见
jb.addActionListener(new ActionListener() {
// 添加按钮事件
@Override
public void actionPerformed(ActionEvent e) {
stop = true;
System.out.println("停止按钮被选中");
}
});
container.add(jb);
this.setBounds(350, 300, 600, 300);// 窗口的坐标和大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 响应关闭事件
this.setVisible(true);// 窗口可视
}
public static void main(String[] args) {
test tmp = new test();
for (int i = 0; i < tmp.bt.length; i++)
tmp.th[i].start();
}
}
class Button extends JButton implements Runnable {
/**
* 按钮类
*/
private static final long serialVersionUID = 1L;
public Button(String text) {
super(text);
setBorderPainted(false);// 边框不可见//比较好看
;
}
@Override
public void run() {
while (!test.stop) {
int x = (int) (Math.random() * 10);
System.out.println("按钮数据由" + getText() + "变为" + x);
this.setText(Integer.toString(x));
// this.setFont(new Font(Integer.toString(x), 1, 18));// 不知道为啥不行
// 改变文本卡了好久〒▽〒
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}