shibazi

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.多线程实例

package second;

public class A implements Runnable {
    public char stat = '*';
    public void run(){
        for(int i = 1;i <= 10 ; i++){
            for(int y=1;y<=i;y++){
                System.out.print(stat);
            }
            System.out.println();
        }
    }
    public static void main(String args[]){
        A a1 = new A();
        Thread b1 = new Thread(a1);
        A a2 = new A();
        Thread b2 = new Thread(a2);
        A a3 = new A();
        Thread b3 = new Thread(a3);
        a1.stat = '+';
        a2.stat = '-';
        a3.stat = '*';
        b1.start();
        b2.start();
        b3.start();
    }
}

输出结果:

-
--
---
---*
**
***
****
*****
******
*******
********
*********
**********
+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
+-
++++-----
------
-------
--------
---------
----------
+++++

PS::可以看出结果是很无序的,证明是同步在运行的!

posted on 2014-07-15 12:06  一生学习,何来毕业  阅读(854)  评论(0编辑  收藏  举报