java中线程真的是一个好东西,就是这个机制,让我们实现了多任务的应用执行模式,但是也有一个缺点:那就是线程的具体执行是无法确定的,完全是由你的系统或者java虚拟机来分配时间,你是没有决定权的!真是恼人的缺点,不过好像那个优先级可以让我们把对线程的执行权利重新掌握到我们自己的手中。下面是我学的第一个多线程编程的例子:
package xinyu.shangrao.demo.fucking;
/*这是通过继承Thread类实现的方式*/
public class TestDemo extends Thread
{
public void run(){
for(int i=0;i<10;i++){
System.out.println("hello Thread Name:"+Thread.currentThread().getName());
}
}
public static void main(String[] args){
TestDemo demo = new TestDemo();
TestDemo demo1 = new TestDemo();
demo.setName("DEMORICK");
demo1.setName("DEMOs");
System.out.println("before start thread is live ?:"
+Thread.currentThread().isAlive());
System.out.println("before start thread is live ?:"
+Thread.currentThread().isAlive());
demo1.start();
demo.start();
System.out.println("just after start live:"+Thread.currentThread().isAlive());
for(int i=0;i<10;i++){
System.out.println("Thread Name now:"+Thread.currentThread().getName());
}
System.out.println("end of main:"+Thread.currentThread().getName());
}
}
运行结果如下:
before start thread is live ?:true
before start thread is live ?:true
just after start live:true
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
Thread Name now:main
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
end of main:main
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMORICK
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
hello Thread Name:DEMOs
注意这个运行结果的线程先后顺序可能不是唯一的!!
run方法是创建的线程对象的执行主体,而main方法则是由系统调用的主线程。
这个程序显示多线程的具体运行方式:那就是给每个线程都分配同样的时间,轮流执行每个线程直到结束。
下面是第二个程序,通过实现Runnable接口:
package xinyu.shangrao.demo.fucking;
import java.applet.Applet;
import java.awt.Graphics;
public class TestDemo extends Applet implements Runnable
{
/**
*
*/
private static final long serialVersionUID = 9070525798365769829L;
Thread my_thread;
int x1=0,y1=0 ;
final int x2=100,y2=150;
public void init(){
setSize(130,200);
setVisible(true);
//检测程序开始运行时的线程名字
System.out.println("the start of applet:"+Thread.currentThread().getName());
}
public void start(){
my_thread = new Thread(this);
my_thread.setName("myDemo");
my_thread.start();
System.out.println("start Thread Name:"+Thread.currentThread().getName());
}
public void run(){
System.out.println("before start thread is live ?:"
+Thread.currentThread().isAlive());
repaint();
try{
Thread.sleep(10);// isInterrupted()方法不能用于检测sleep方法,因为这是无效的
//interrupted()方法和isInterupted()方法还是有点区别的,注意区分
System.out.println("is interrupt?:"+my_thread.isInterrupted());
}catch(InterruptedException e){
}
System.out.println("hello Thread Name:"+Thread.currentThread().getName());
}
public void paint(Graphics g){
if(x1>100)x1 = 0;
x1 += 10;
y1 += 10;
g.drawOval(x1, y1, x2, y2);
System.out.println("Thread Name now:"+Thread.currentThread().getName());
}
public void stop(){
System.out.println("end live ?:"
+Thread.currentThread().isAlive());
}
public void destroy(){
if(my_thread != null)
my_thread=null; //关闭线程对象
}
}
运行结果如下:
the start of applet:thread applet-xinyu.shangrao.demo.fucking.TestDemo.class
Thread Name now:AWT-EventQueue-1
before start thread is live ?:true
Thread Name now:AWT-EventQueue-1
is interrupt?:false
hello Thread Name:myDemo
Thread Name now:AWT-EventQueue-1
end live ?:true
这个运行结果我也不好分析出具体情况,如果你分析出来了,可以告诉我哦,我的QQ是1094520627,邮箱也是用QQ邮箱的,如果能够告诉我为什么是这样的运行结果本人不胜感激。谢谢!