1
package
org.zsk.error;
2
3 public class MultiThreadDemo1_1 {
4
5 public MultiThreadDemo1_1() {
6 // TODO Auto-generated constructor stub
7 new NewThread( " 1 " );
8 new NewThread( " 2 " );
9 new NewThread( " 3 " );
10 new NewThread( " 4 " );
11
12 System.out.println( " main thread begin! " );
13 for ( int i = 0 ; i < 100 ; i ++ ){
14 System.out.println( " --> " );
15
16 }
17 System.out.println( " main thread end; " );
18
19 }
20
21 /**
22 * @param args
23 */
24 public static void main(String[] args) {
25 // TODO Auto-generated method stub
26 new MultiThreadDemo1_1();
27 }
28
29 class NewThread implements Runnable {
30 NewThread(String threadName){
31 name = threadName;
32 t = new Thread( this , name);
33 t.start();
34 System.out.println( " new thread " + name + " begin " );
35 }
36
37 public void run(){
38 try {
39 for ( int i = 0 ; i < 100 ; i ++ ){
40 System.out.println(name);
41 Thread.sleep( 30 );
42 }
43 } catch (InterruptedException e) {
44 System.out.println( " thread " + name + " error! " );
45 }
46
47 System.out.println( " thread " + name + " end; " );
48 }
49
50 private String name;
51 private Thread t;
52 }
53
54 }
2
3 public class MultiThreadDemo1_1 {
4
5 public MultiThreadDemo1_1() {
6 // TODO Auto-generated constructor stub
7 new NewThread( " 1 " );
8 new NewThread( " 2 " );
9 new NewThread( " 3 " );
10 new NewThread( " 4 " );
11
12 System.out.println( " main thread begin! " );
13 for ( int i = 0 ; i < 100 ; i ++ ){
14 System.out.println( " --> " );
15
16 }
17 System.out.println( " main thread end; " );
18
19 }
20
21 /**
22 * @param args
23 */
24 public static void main(String[] args) {
25 // TODO Auto-generated method stub
26 new MultiThreadDemo1_1();
27 }
28
29 class NewThread implements Runnable {
30 NewThread(String threadName){
31 name = threadName;
32 t = new Thread( this , name);
33 t.start();
34 System.out.println( " new thread " + name + " begin " );
35 }
36
37 public void run(){
38 try {
39 for ( int i = 0 ; i < 100 ; i ++ ){
40 System.out.println(name);
41 Thread.sleep( 30 );
42 }
43 } catch (InterruptedException e) {
44 System.out.println( " thread " + name + " error! " );
45 }
46
47 System.out.println( " thread " + name + " end; " );
48 }
49
50 private String name;
51 private Thread t;
52 }
53
54 }