Java多线程编码,顺序执行(同步)
java线程实现机制:
1.继承Thread类,并实现run方法。2.传递Runnable类。
例如:
public class TestThread extends Thread
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {
public void run() // 继承Thread后必须实现的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
for ( int i = 0 ; i < 3 ; i ++ )
System.out.println(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
public static void main(String [] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
TestThread[] tt = new TestThread[ 10 ]; // new 线程数组
for ( int i = 0 ;i < 10 ;i ++ )
tt[i] = new TestThread(i); // new 线程
for ( int i = 0 ;i < 10 ;i ++ )
tt[i].start(); // 线程启动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }
创建线程调用的是默认的构造函数.启动线程是使用的start()函数,这个是父类Thread的方法。start()被调用后,回自动的调用run()方法。另一种
创建线程的方法:
public static void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) Thread thread2 = new Thread( new Runnable()
… {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) public void run() … { for ( int i = 0
; i < 5 ; i ++ ) System.out.println(i); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) } );
thread2.start();//启动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }
有时候我们想让刚才的打印按顺序的输出。如果把刚才的run函数中的for循环10次以上,可能将不会是想要的顺序输出的。要使得线程按顺序输出采用synchron
ized 关键字。例如下面的函数
public class TestThread extends Thread
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {
int count = 0 ;
public TestThread( int id)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
count = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
public void run()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
print(count);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
public void main(String [] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
TestThread[] tt = new TestThread[ 10 ];
for ( int i = 0 ;i < 10 ;i ++ )
tt[i] = new TestThread(i);
for ( int i = 0 ;i < 10 ;i ++ )
tt[i].start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
public synchronized void print( int i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
for ( int q = 0 ; q < 100 ; q ++ )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {
System.out.println(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }
这里使用了 synchronized 关键字。但是打印出来的东西依然不是有序的。这并不是 synchronized 出现了问题。而是 synchr
onized用于锁定一个对象,或者对象的一个方法。但是对于以上的那个程序。每次new一个TestThread都产生了一个新的对象。而每一个新的对象也有一个p
rint方法。这就没有使 synchronized发挥作用。如果这个print方法前加入了 static 关键字,这个程序便有序输出。
同时也说明java与c的小小不同。在c中类的函数并不是归属于类的。也就是说c++的函数有唯一的入口地址。而java则有些不同。不知道是不是每一个非s
tatic函数都有对于对象而言的,也不知道是不是每生产一个对象都有一个函数地址。有待研究。。。。。