package com.swing.japplet;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Test9 extends JApplet {
JProgressBar pb = new JProgressBar();
public void init() {
Container contentPane = getContentPane();
final JButton startButton = new JButton("start");
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(pb);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GetInfoThread t = new GetInfoThread(Test9.this);
t.start();
// this is ok, because actionPerformed
// is called on the event dispatch thread
startButton.setEnabled(false);
}
});
}
public JProgressBar getProgressBar() {
return pb;
}
}
class GetInfoThread extends Thread {
Runnable runnable;
int value; public GetInfoThread(final Test9 test9) {
runnable = new Runnable() {
public void run() {
JProgressBar pb = test9.getProgressBar();
pb.setValue(value);
}
};
} public void run() {
while (true) {
try {
Thread.currentThread().sleep(500);
value = (int) (Math.random() * 100);
SwingUtilities.invokeLater(runnable);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}这段代码可以运行,虽然也debug跟踪过了,但还是有些不理解其运行机制,请求帮忙我主要不能理解其红色代码部分
这部分代码什么时候执行,其里面的run方法和后面的蓝色部分的run()方法有什么不同
请求解答
import java.awt.*;
import java.awt.event.*;public class Test9 extends JApplet {
JProgressBar pb = new JProgressBar();
public void init() {
Container contentPane = getContentPane();
final JButton startButton = new JButton("start");
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(pb);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GetInfoThread t = new GetInfoThread(Test9.this);
t.start();
// this is ok, because actionPerformed
// is called on the event dispatch thread
startButton.setEnabled(false);
}
});
}
public JProgressBar getProgressBar() {
return pb;
}
}
class GetInfoThread extends Thread {
Runnable runnable;
int value; public GetInfoThread(final Test9 test9) {
runnable = new Runnable() {
public void run() {
JProgressBar pb = test9.getProgressBar();
pb.setValue(value);
}
};
} public void run() {
while (true) {
try {
Thread.currentThread().sleep(500);
value = (int) (Math.random() * 100);
SwingUtilities.invokeLater(runnable);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}这段代码可以运行,虽然也debug跟踪过了,但还是有些不理解其运行机制,请求帮忙我主要不能理解其红色代码部分
这部分代码什么时候执行,其里面的run方法和后面的蓝色部分的run()方法有什么不同
请求解答
红色部分是另一个自定义线程的初始化,就是说自定义线程建立的时候另一个自定义线程就被创建了,但是没有开始运行。
蓝色部分相当于调用了 Thread 的 start 方法,开始执行由线程创建的那个线程。
import java.awt.*;
import java.awt.event.*;public class Test9 extends JApplet {
JProgressBar pb = new JProgressBar();
public void init() {
Container contentPane = getContentPane();
final JButton startButton = new JButton("start");
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(pb);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GetInfoThread t = new GetInfoThread(Test9.this);
t.start();
// this is ok, because actionPerformed
// is called on the event dispatch thread
startButton.setEnabled(false);
}
});
}
public JProgressBar getProgressBar() {
return pb;
}
}
class GetInfoThread extends Thread {
Runnable runnable;
int value; public GetInfoThread(final Test9 test9) {
runnable = new Runnable() {
public void run() {
JProgressBar pb = test9.getProgressBar();
pb.setValue(value);
}
};
} public void run() {
while (true) {
try {
Thread.currentThread().sleep(500);
value = (int) (Math.random() * 100);
SwingUtilities.invokeLater(runnable);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Queue q=new Queue ();//new出一个q:后面的两个线程都是用的同一个q,保证一个put一个get
Producer p=new Producer (q);//让new出的p去往q里面put
Customer c=new Customer (q);//让new出的c从q中get
p.start();//p和q开始的顺序并不报错
c.start();
}
}
class Producer extends Thread
{
Queue q;
public Producer(Queue q) {
this.q=q;//给成员变量赋值,再一调运q的put方法
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
q.put(i);//此处只是让q去put 10次
System.out.println("Producer put "+i);//并且输出本次放的是第几杯
}
}
}
class Customer extends Thread
{
Queue q;
public Customer(Queue q) {
this.q=q;//给成员变量赋值,再一调运q的get方法
}
@Override
public void run() {
while (true) {//死循环:只要q里面有,就去get
//get方法有返回值,返回值就是producer所put的数量
//此处也不需要去考虑是第几杯
//在Queue中的value解决可这一问题:
//put中的I赋给value,get方法有返回值就value的值
System.out.println("Customer get "+q.get());
//如果循环完了,就跳出循环,否则线程不会自己结束
if (q.value==9) {
break;
}
}
}
}
class Queue
{
int value;
boolean bFull=false;
public synchronized void put (int i)//在producer中的put方法中就是将其I传进来
{
if (!bFull) {//条件为真(如果没满,就倒水)
value=i;//给value赋值,现在有几杯水
bFull=true;//满了
notify();//唤醒其他线程(让customer去get)
}
try {
wait();//告诉customer去get后自己等待customer的get结束
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized int get()
{
if (!bFull) {//如果没满就等待,如果满了就不进 **这就是为什么main里面谁先开始不报错的原因**
//get和put方法中的if条件判断起到了至关重要的作用
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bFull =false;//赋值为没满
notify();//唤醒producer去put
return value;//get的返回值就是put的时候给value赋的值
}
}