马士兵J2SE-第九章-多线程机制-线程的创建与启动

 
public class test{
	public static void main(String[] args) {
		m1();
	}
	
	public static void m1() {
		m2();
		m3();
	}
	
	public static void m2() {
	
	}
	
	public static void m3() {
		
	}
}

进程是一个静态的概念,在我们机器里,实际运行的都是线程。



线程启动:

package com.zzk.test;

public class Test {
    public static void main(String[] args) {
    	Runner1 r=new Runner1();
    	//r.run();
    	Thread t=new Thread(r);
    	t.start();
    	
    	for(int i=0;i<100;i++) {
    		System.out.println("Main Thread:-----"+i);
    	}
    }
}



class Runner1 implements Runnable {
	public void run() {
		for(int i=0;i<100;i++) {
			System.out.println("Runner1 :"+i);
		}
	}
}

输出:

Main Thread:-----0
Runner1 :0
Main Thread:-----1
Runner1 :1
Main Thread:-----2
Runner1 :2
Main Thread:-----3
Runner1 :3
Runner1 :4
Runner1 :5
Main Thread:-----4
Runner1 :6
Main Thread:-----5
Runner1 :7
Main Thread:-----6
Runner1 :8
Main Thread:-----7
Runner1 :9
Main Thread:-----8
Runner1 :10
Main Thread:-----9
Runner1 :11
Main Thread:-----10
Runner1 :12
Main Thread:-----11
Runner1 :13
Main Thread:-----12
Runner1 :14
Main Thread:-----13
Runner1 :15
Main Thread:-----14
Runner1 :16
Runner1 :17
Runner1 :18
Runner1 :19
Main Thread:-----15
Main Thread:-----16
Main Thread:-----17
Main Thread:-----18
Main Thread:-----19
Main Thread:-----20
Main Thread:-----21
Main Thread:-----22
Main Thread:-----23
Main Thread:-----24
Main Thread:-----25
Main Thread:-----26
Main Thread:-----27
Main Thread:-----28
Main Thread:-----29
Main Thread:-----30
Main Thread:-----31
Main Thread:-----32
Main Thread:-----33
Main Thread:-----34
Main Thread:-----35
Runner1 :20
Main Thread:-----36
Runner1 :21
Main Thread:-----37
Runner1 :22
Main Thread:-----38
Runner1 :23
Main Thread:-----39
Runner1 :24
Main Thread:-----40
Runner1 :25
Main Thread:-----41
Runner1 :26
Main Thread:-----42
Main Thread:-----43
Main Thread:-----44
Main Thread:-----45
Main Thread:-----46
Main Thread:-----47
Runner1 :27
Main Thread:-----48
Runner1 :28
Runner1 :29
Main Thread:-----49
Runner1 :30
Runner1 :31
Runner1 :32
Runner1 :33
Runner1 :34
Runner1 :35
Runner1 :36
Runner1 :37
Runner1 :38
Runner1 :39
Runner1 :40
Runner1 :41
Runner1 :42
Runner1 :43
Runner1 :44
Runner1 :45
Runner1 :46
Runner1 :47
Runner1 :48
Runner1 :49
Runner1 :50
Main Thread:-----50
Runner1 :51
Main Thread:-----51
Runner1 :52
Main Thread:-----52
Runner1 :53
Main Thread:-----53
Runner1 :54
Main Thread:-----54
Runner1 :55
Main Thread:-----55
Runner1 :56
Main Thread:-----56
Runner1 :57
Main Thread:-----57
Runner1 :58
Main Thread:-----58
Runner1 :59
Main Thread:-----59
Runner1 :60
Main Thread:-----60
Runner1 :61
Main Thread:-----61
Runner1 :62
Main Thread:-----62
Runner1 :63
Main Thread:-----63
Runner1 :64
Main Thread:-----64
Runner1 :65
Main Thread:-----65
Runner1 :66
Main Thread:-----66
Runner1 :67
Main Thread:-----67
Runner1 :68
Main Thread:-----68
Runner1 :69
Main Thread:-----69
Runner1 :70
Main Thread:-----70
Runner1 :71
Main Thread:-----71
Runner1 :72
Main Thread:-----72
Runner1 :73
Runner1 :74
Runner1 :75
Runner1 :76
Runner1 :77
Main Thread:-----73
Runner1 :78
Main Thread:-----74
Runner1 :79
Main Thread:-----75
Runner1 :80
Main Thread:-----76
Runner1 :81
Main Thread:-----77
Runner1 :82
Main Thread:-----78
Runner1 :83
Main Thread:-----79
Runner1 :84
Main Thread:-----80
Runner1 :85
Main Thread:-----81
Runner1 :86
Main Thread:-----82
Runner1 :87
Main Thread:-----83
Runner1 :88
Main Thread:-----84
Runner1 :89
Main Thread:-----85
Runner1 :90
Main Thread:-----86
Runner1 :91
Main Thread:-----87
Runner1 :92
Runner1 :93
Runner1 :94
Runner1 :95
Runner1 :96
Runner1 :97
Runner1 :98
Main Thread:-----88
Runner1 :99
Main Thread:-----89
Main Thread:-----90
Main Thread:-----91
Main Thread:-----92
Main Thread:-----93
Main Thread:-----94
Main Thread:-----95
Main Thread:-----96
Main Thread:-----97
Main Thread:-----98
Main Thread:-----99


方法调用:

package com.zzk.test;

public class Test {
    public static void main(String[] args) {
    	Runner1 r=new Runner1();
    	r.run();
    	//Thread t=new Thread(r);
    	//t.start();
    	
    	for(int i=0;i<100;i++) {
    		System.out.println("Main Thread:-----"+i);
    	}
    }
}



class Runner1 implements Runnable {
	public void run() {
		for(int i=0;i<100;i++) {
			System.out.println("Runner1 :"+i);
		}
	}
}

输出:

Runner1 :0
Runner1 :1
Runner1 :2
Runner1 :3
Runner1 :4
Runner1 :5
Runner1 :6
Runner1 :7
Runner1 :8
Runner1 :9
Runner1 :10
Runner1 :11
Runner1 :12
Runner1 :13
Runner1 :14
Runner1 :15
Runner1 :16
Runner1 :17
Runner1 :18
Runner1 :19
Runner1 :20
Runner1 :21
Runner1 :22
Runner1 :23
Runner1 :24
Runner1 :25
Runner1 :26
Runner1 :27
Runner1 :28
Runner1 :29
Runner1 :30
Runner1 :31
Runner1 :32
Runner1 :33
Runner1 :34
Runner1 :35
Runner1 :36
Runner1 :37
Runner1 :38
Runner1 :39
Runner1 :40
Runner1 :41
Runner1 :42
Runner1 :43
Runner1 :44
Runner1 :45
Runner1 :46
Runner1 :47
Runner1 :48
Runner1 :49
Runner1 :50
Runner1 :51
Runner1 :52
Runner1 :53
Runner1 :54
Runner1 :55
Runner1 :56
Runner1 :57
Runner1 :58
Runner1 :59
Runner1 :60
Runner1 :61
Runner1 :62
Runner1 :63
Runner1 :64
Runner1 :65
Runner1 :66
Runner1 :67
Runner1 :68
Runner1 :69
Runner1 :70
Runner1 :71
Runner1 :72
Runner1 :73
Runner1 :74
Runner1 :75
Runner1 :76
Runner1 :77
Runner1 :78
Runner1 :79
Runner1 :80
Runner1 :81
Runner1 :82
Runner1 :83
Runner1 :84
Runner1 :85
Runner1 :86
Runner1 :87
Runner1 :88
Runner1 :89
Runner1 :90
Runner1 :91
Runner1 :92
Runner1 :93
Runner1 :94
Runner1 :95
Runner1 :96
Runner1 :97
Runner1 :98
Runner1 :99
Main Thread:-----0
Main Thread:-----1
Main Thread:-----2
Main Thread:-----3
Main Thread:-----4
Main Thread:-----5
Main Thread:-----6
Main Thread:-----7
Main Thread:-----8
Main Thread:-----9
Main Thread:-----10
Main Thread:-----11
Main Thread:-----12
Main Thread:-----13
Main Thread:-----14
Main Thread:-----15
Main Thread:-----16
Main Thread:-----17
Main Thread:-----18
Main Thread:-----19
Main Thread:-----20
Main Thread:-----21
Main Thread:-----22
Main Thread:-----23
Main Thread:-----24
Main Thread:-----25
Main Thread:-----26
Main Thread:-----27
Main Thread:-----28
Main Thread:-----29
Main Thread:-----30
Main Thread:-----31
Main Thread:-----32
Main Thread:-----33
Main Thread:-----34
Main Thread:-----35
Main Thread:-----36
Main Thread:-----37
Main Thread:-----38
Main Thread:-----39
Main Thread:-----40
Main Thread:-----41
Main Thread:-----42
Main Thread:-----43
Main Thread:-----44
Main Thread:-----45
Main Thread:-----46
Main Thread:-----47
Main Thread:-----48
Main Thread:-----49
Main Thread:-----50
Main Thread:-----51
Main Thread:-----52
Main Thread:-----53
Main Thread:-----54
Main Thread:-----55
Main Thread:-----56
Main Thread:-----57
Main Thread:-----58
Main Thread:-----59
Main Thread:-----60
Main Thread:-----61
Main Thread:-----62
Main Thread:-----63
Main Thread:-----64
Main Thread:-----65
Main Thread:-----66
Main Thread:-----67
Main Thread:-----68
Main Thread:-----69
Main Thread:-----70
Main Thread:-----71
Main Thread:-----72
Main Thread:-----73
Main Thread:-----74
Main Thread:-----75
Main Thread:-----76
Main Thread:-----77
Main Thread:-----78
Main Thread:-----79
Main Thread:-----80
Main Thread:-----81
Main Thread:-----82
Main Thread:-----83
Main Thread:-----84
Main Thread:-----85
Main Thread:-----86
Main Thread:-----87
Main Thread:-----88
Main Thread:-----89
Main Thread:-----90
Main Thread:-----91
Main Thread:-----92
Main Thread:-----93
Main Thread:-----94
Main Thread:-----95
Main Thread:-----96
Main Thread:-----97
Main Thread:-----98
Main Thread:-----99


第二种创建方式

package com.zzk.test;

public class Test {
    public static void main(String[] args) {
    	Runner1 r=new Runner1();
    	//r.run();
    	//Thread t=new Thread(r);
    	r.start();
    	
    	for(int i=0;i<100;i++) {
    		System.out.println("Main Thread:-----"+i);
    	}
    }
}



class Runner1 extends Thread {
	public void run() {
		for(int i=0;i<100;i++) {
			System.out.println("Runner1 :"+i);
		}
	}
}



 



上一篇:Centos RSync+Shell实现数据定时同步


下一篇:阿里云贵州教育脱贫正式展开,黔东南百位脱贫一线技术人员参加培训