以下是我的程序:
package threadtest2;import java.awt.BorderLayout;
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.TextArea;
import java.awt.*;/**
* <p>Title: ThreadTest2</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ThreadTest3 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
TextArea textArea1 = new TextArea(); public ThreadTest3() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
class DemoThread implements Runnable{
String name;
Thread t;
DemoThread(String Name_of_Thread){
name=Name_of_Thread;
t=new Thread(this.name);
textArea1.append("New thread:"+t+"\n");
t.start();
}
public void run(){
try{
for (int i=4;i>0;i--){
textArea1.append(name+":"+i+"\n");
Thread.sleep(1000);
}
}catch(InterruptedException e){
textArea1.append(name + "interuupted.\n");
}
textArea1.append(name +"exied.\n");
} }
DemoThread th1=new DemoThread("one");
DemoThread th2=new DemoThread("two");
textArea1.append("Thread one is alive:"+th1.t.isAlive()+"\n");
textArea1.append("Thread two is alive:"+th2.t.isAlive()+"\n");
try{
textArea1.append("waiting for threads to finish.\n");
th1.t.join();
th2.t.join();
}
catch(InterruptedException e){
textArea1.append("Main thread interrupted\n");
}
textArea1.append("Thread one is alive:"+th1.t.isAlive()+"\n");
textArea1.append("Thread two is alive:"+th2.t.isAlive()+"\n");
textArea1.append("Main thread exited."); contentPane.add(textArea1, java.awt.BorderLayout.CENTER);
}
}
但我不知道为什么它可以执行到t.start(),但就是进不了 run(),请高手们帮帮忙~~~~~~~~~
package threadtest2;import java.awt.BorderLayout;
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.TextArea;
import java.awt.*;/**
* <p>Title: ThreadTest2</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ThreadTest3 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
TextArea textArea1 = new TextArea(); public ThreadTest3() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
class DemoThread implements Runnable{
String name;
Thread t;
DemoThread(String Name_of_Thread){
name=Name_of_Thread;
t=new Thread(this.name);
textArea1.append("New thread:"+t+"\n");
t.start();
}
public void run(){
try{
for (int i=4;i>0;i--){
textArea1.append(name+":"+i+"\n");
Thread.sleep(1000);
}
}catch(InterruptedException e){
textArea1.append(name + "interuupted.\n");
}
textArea1.append(name +"exied.\n");
} }
DemoThread th1=new DemoThread("one");
DemoThread th2=new DemoThread("two");
textArea1.append("Thread one is alive:"+th1.t.isAlive()+"\n");
textArea1.append("Thread two is alive:"+th2.t.isAlive()+"\n");
try{
textArea1.append("waiting for threads to finish.\n");
th1.t.join();
th2.t.join();
}
catch(InterruptedException e){
textArea1.append("Main thread interrupted\n");
}
textArea1.append("Thread one is alive:"+th1.t.isAlive()+"\n");
textArea1.append("Thread two is alive:"+th2.t.isAlive()+"\n");
textArea1.append("Main thread exited."); contentPane.add(textArea1, java.awt.BorderLayout.CENTER);
}
}
但我不知道为什么它可以执行到t.start(),但就是进不了 run(),请高手们帮帮忙~~~~~~~~~
DemoThread th1=new DemoThread("one");
DemoThread th2=new DemoThread("two");
new Thread(th1).start();另外,为什么不用extends Thread?至少可以不用Thread成员
t=new Thread(this.name);
textArea1.append("New thread:"+t+"\n");
t.start();
并没有启动你设计的线程。如果你想启动一个自定义的线程,应该用下面的方式启动。
Thread t = new Thread(new 自定义线程());
t.start();