1、完成书上268页习题第7题和实验题第1、2题
(1)第7题
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RollWords extends JFrame{
static RollWords.MyThread thread1, thread2; //成员内部类
private void showMe(){
setLayout(new GridLayout(4,1));
setBackground(Color.LIGHT_GRAY);
setBounds(200,140,400,240);
setVisible(true); //显示窗口
}
private class MyThread extends Thread{ //内部线程类 继承
//线程中要用到的组件
private JLabel label;
private JTextField text1,text2;
private JButton start,interrupt;
private int sleepTime;
public MyThread(String str){//创建界面
super(str);
//初始化字符串,文字后+若干空格
text1 = new JTextField();
for(int i=0; i<100; i++){
str +=" ";
}
text1 = new JTextField(str);
sleepTime=(int)(Math.random()*100);//随机休眠xxms
label=new JLabel("sleep");
text2 = new JTextField(""+sleepTime);
start = new JButton("启动");
interrupt = new JButton("停止");
init(); //在多线程运用中,启动一个线程之前要对一个对象进行一些初始化操作
start.addActionListener(new MyListener());
interrupt.addActionListener(new MyListener());
}
private void init(){
add(text1);
JPanel panel;
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(label);
panel.add(text2);
panel.add(start);
panel.add(interrupt);
add(panel);
setVisible(true); //显示线程中的组件
}
public void run(){ //线程方法
String str;
while(true){
try{
str = text1.getText();
str = str.substring(1)+str.substring(0,1); //字符串滚动
text1.setText(str);
Thread.sleep(sleepTime);
}catch(InterruptedException e){//睡眠期间被中断(按钮),发生异常
break;
}
}
}
private class MyListener implements ActionListener{//内部类,处理事件监听,为线程类中的按钮服务
public void actionPerformed(ActionEvent e){
if(e.getSource()==thread1.start || e.getSource()==thread1.interrupt)
actionPerformed(e, thread1);
if(e.getSource()==thread2.start || e.getSource()==thread2.interrupt)
actionPerformed(e, thread2);
}
private void actionPerformed(ActionEvent e, MyThread thread){
if(e.getSource()==thread.start){//启动
thread.sleepTime = Integer.parseInt(thread.text2.getText());
thread.start();
thread.start.setEnabled(false);
}
if(e.getSource()==thread.interrupt){//打断
thread.interrupt();
thread.interrupt.setEnabled(false);
}
}
}
}
public static void main(String[] args){
RollWords w = new RollWords();
w.showMe();
thread1 = w.new MyThread("welcome!");
thread2 = w.new MyThread("How are you!");
}
}
(2)实验1
package chap11.test.test1;
public class PrintCharacter implements Runnable{
private int i=0;
public static void main(String[] args) {
PrintCharacter pc = new PrintCharacter();
new Thread(pc).start();
new Thread(pc).start();
new Thread(pc).start();
}
public void run() {
while(i<26){
synchronized(this){//原子操作进行加锁,锁作为对象监听
if(i>=26) break;
System.out.print(Thread.currentThread().getName()+" ");
System.out.println((char)(i+'a'));//a开始
try {
Thread.sleep(100);
} catch (InterruptedException e) {//可能抛出的异常
e.printStackTrace();
}
i++;
}
}
}
}
(3)实验2
package chap11.test.test2;
public class Number extends Thread{
private Object message;
public Number(Object message) {
super();
this.message = message;
}
public void run(){
//1-52
synchronized(message){
for(int i=1; i<=52; i++){
System.out.println(i+" ");
if(i%2==0){
message.notifyAll();
try {
message.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
package chap11.test.test2;
public class Char extends Thread{
private Object message;
public Char(Object message) {
super();
this.message = message;
}
public void run(){
//a-z
synchronized(message){
for(int i=0; i<26; i++){
System.out.println((char)(i+'a')+" ");
message.notifyAll();
try {
message.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package chap11.test.test2;
public class Test {
public static void main(String[] args) {
Object message = new Object();
Thread t1 = new Number(message);
Thread t2 = new Char(message);
t1.start();
t2.start();
}
}