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::可以看出结果是很无序的,证明是同步在运行的!