马士兵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);
		}
	}
}



 



目录
相关文章
|
1月前
|
存储 消息中间件 资源调度
C++ 多线程之初识多线程
这篇文章介绍了C++多线程的基本概念,包括进程和线程的定义、并发的实现方式,以及如何在C++中创建和管理线程,包括使用`std::thread`库、线程的join和detach方法,并通过示例代码展示了如何创建和使用多线程。
43 1
C++ 多线程之初识多线程
|
24天前
|
Java 开发者
在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口
【10月更文挑战第20天】在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口。本文揭示了这两种方式的微妙差异和潜在陷阱,帮助你更好地理解和选择适合项目需求的线程创建方式。
19 3
|
24天前
|
Java 开发者
在Java多线程编程中,选择合适的线程创建方法至关重要
【10月更文挑战第20天】在Java多线程编程中,选择合适的线程创建方法至关重要。本文通过案例分析,探讨了继承Thread类和实现Runnable接口两种方法的优缺点及适用场景,帮助开发者做出明智的选择。
16 2
|
24天前
|
Java
Java中多线程编程的基本概念和创建线程的两种主要方式:继承Thread类和实现Runnable接口
【10月更文挑战第20天】《JAVA多线程深度解析:线程的创建之路》介绍了Java中多线程编程的基本概念和创建线程的两种主要方式:继承Thread类和实现Runnable接口。文章详细讲解了每种方式的实现方法、优缺点及适用场景,帮助读者更好地理解和掌握多线程编程技术,为复杂任务的高效处理奠定基础。
28 2
|
24天前
|
Java 开发者
Java多线程初学者指南:介绍通过继承Thread类与实现Runnable接口两种方式创建线程的方法及其优缺点
【10月更文挑战第20天】Java多线程初学者指南:介绍通过继承Thread类与实现Runnable接口两种方式创建线程的方法及其优缺点,重点解析为何实现Runnable接口更具灵活性、资源共享及易于管理的优势。
28 1
|
24天前
|
安全 Java 开发者
Java多线程中的`wait()`、`notify()`和`notifyAll()`方法,探讨了它们在实现线程间通信和同步中的关键作用
本文深入解析了Java多线程中的`wait()`、`notify()`和`notifyAll()`方法,探讨了它们在实现线程间通信和同步中的关键作用。通过示例代码展示了如何正确使用这些方法,并分享了最佳实践,帮助开发者避免常见陷阱,提高多线程程序的稳定性和效率。
34 1
|
24天前
|
Java
在Java多线程编程中,`wait()` 和 `notify()/notifyAll()` 方法是线程间通信的核心机制。
在Java多线程编程中,`wait()` 和 `notify()/notifyAll()` 方法是线程间通信的核心机制。它们通过基于锁的方式,使线程在条件不满足时进入休眠状态,并在条件成立时被唤醒,从而有效解决数据一致性和同步问题。本文通过对比其他通信机制,展示了 `wait()` 和 `notify()` 的优势,并通过生产者-消费者模型的示例代码,详细说明了其使用方法和重要性。
25 1
|
2月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
1月前
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
45 6
|
1月前
|
存储 运维 NoSQL
Redis为什么最开始被设计成单线程而不是多线程
总之,Redis采用单线程设计是基于对系统特性的深刻洞察和权衡的结果。这种设计不仅保持了Redis的高性能,还确保了其代码的简洁性、可维护性以及部署的便捷性,使之成为众多应用场景下的首选数据存储解决方案。
41 1