`
i拾贝
  • 浏览: 29951 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java中的线程Thread

    博客分类:
  • java
阅读更多

java中的线程Thread

java.lang

Class Thread

 线程的概念:

线程是一个程序内部的程序控制流。

 

主线程方法测试,main()

T.java

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

 线程图,只有一个线程



 

测试线程和方法

通过实现Runnable接口实现一个线程,启动线程时需要new一个Thread,调用thread的start方法

TestThread1.java

/*
	测试线程1
*/
public class TestThread1 {
	public static void main(String args[]) {
		Runner1 r = new Runner1();
		//调用run()方法
		r.run();
		//Thread t = new Thread(r);
		//t.start();
		
		for(int i=0; i<100; i++) {
			System.out.println("Main Thread:------" + i);
		}
	}
}

//实现Runnable接口实现一个线程
class Runner1 implements Runnable {
	public void run() {
		for(int i=0; i<100; i++) {	
			System.out.println("Runner1 :" + i);
		}
	}
}

 

F:\java\Thread>javac TestThread1.java

F:\java\Thread>java TestThread1
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

F:\java\Thread>

 按主线程main()顺序,线执行run()方法,在执行main方法后面的内容。

 

使用Thread,测试

TestThread1.java

/*
	测试线程1
*/
public class TestThread1 {
	public static void main(String args[]) {
		Runner1 r = new Runner1();
		//调用run()方法
		//r.run();
		
		Thread t = new Thread(r);
		//开始一个新的分支,与main方法并行执行
		t.start();
		
		for(int i=0; i<100; i++) {
			System.out.println("Main Thread:------" + i);
		}
	}
}

//实现Runnable接口实现一个线程
class Runner1 implements Runnable {
	public void run() {
		for(int i=0; i<100; i++) {	
			System.out.println("Runner1 :" + i);
		}
	}
}

 

F:\java\Thread>javac TestThread1.java

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

F:\java\Thread>

 并行执行。

 

 

  • 大小: 121.4 KB
分享到:
评论

相关推荐

    java多线程Demo

    Java线程有10个优先级(MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY),默认优先级是NORM_PRIORITY。但是,线程优先级并不保证绝对的执行顺序,操作系统调度策略可能影响实际执行顺序。 7. join()方法: 一个线程...

    Java 模拟线程并发

    在Java中,线程并发可以通过多种方式实现,包括继承Thread类、实现Runnable接口以及使用ExecutorService和Future等高级API。下面将详细探讨这些知识点。 首先,Java中创建线程主要有两种方法。一种是通过继承Thread...

    java多线程进度条

    在Java编程中,多线程是一项关键特性,它允许程序同时执行多个任务,提升系统效率。在处理耗时操作如大文件下载、数据处理或网络请求时,展示进度条能够提供用户友好的交互体验,让使用者了解任务的完成状态。本主题...

    Java多线程知识点总结

    Java多线程是Java编程语言中一个非常重要的概念,它允许开发者在一个程序中创建多个执行线程并行运行,以提高程序的执行效率和响应速度。在Java中,线程的生命周期包含五个基本状态,分别是新建状态(New)、就绪...

    java中线程的应用例子

    4. **线程状态**:Java线程有五种状态:新建、可运行、运行、阻塞和死亡。了解这些状态可以帮助开发者更好地控制和调试线程。 5. **线程优先级**:每个线程都有一个优先级,Java中从1(`MIN_PRIORITY`)到10(`MAX_...

    java多线程经典案例

    在Java中,实现多线程有两种主要方式:通过实现Runnable接口或者继承Thread类。本案例将深入探讨Java多线程中的关键知识点,包括线程同步、线程通信和线程阻塞。 线程同步是为了防止多个线程同时访问共享资源,导致...

    java多线程的讲解和实战

    Java多线程是Java编程中的重要概念,尤其在如今的多核处理器环境下,理解并熟练掌握多线程技术对于提高程序性能和响应速度至关重要。本资料详细讲解了Java多线程的原理,并提供了丰富的实战代码,非常适合Java初学者...

    Java-Thread-Affinity,将Java线程绑定到给定的内核.zip

    Java线程亲和性(Thread Affinity)是一个高级并发编程概念,主要涉及到操作系统调度和硬件资源的优化。在多核处理器系统中,线程亲和性允许开发者指定某个线程应该运行在哪个特定的处理器核心上,从而提高性能、...

    java 多线程操作数据库

    在当今高度并发的应用环境中,Java多线程技术被广泛应用于处理数据库操作,以提升系统的响应速度和处理能力。本文将基于一个具体的Java多线程操作数据库的应用程序,深入探讨其背后的原理、实现细节以及潜在的挑战。...

    Java多线程机制(讲述java里面与多线程有关的函数)

    Java多线程机制是Java编程中至关重要的一部分,它允许程序同时执行多个任务,提升应用程序的效率和响应性。以下是对各个知识点的详细说明: 9.1 Java中的线程: Java程序中的线程是在操作系统级别的线程基础上进行...

    线程 JAVA java线程 java线程第3版 java线程第2版第3版合集

    java线程第二版中英文 java线程第二版中英文 线程并不是新的概念:许多操作系统和语言都支持它们。在Java出现以前,似乎人人都在谈论线程,却很少有人使用它。用线程编程是技巧性很强的且不可移植。 而在Java中却...

    java 多线程并发实例

    在Java中,创建线程主要有两种方式:继承Thread类和实现Runnable接口。在提供的实例中,我们可能会看到这两种方式的运用。 1. 继承Thread类:通过创建新的Thread子类并重写其run()方法,然后创建该子类的实例并调用...

    java多线程thread实例

    - Java线程有10个优先级,`MIN_PRIORITY`(1),`NORM_PRIORITY`(5,默认)和`MAX_PRIORITY`(10)。较高的优先级意味着更高的执行概率,但并不保证线程会优先执行。 - `setPriority()`方法可以改变线程的优先级,但...

    Java多线程编程核心技术_完整版_java_

    在Java中,多线程主要通过继承Thread类或实现Runnable接口来实现。本教程《Java多线程编程核心技术》将深入探讨这一主题。 一、线程的创建与启动 1. 继承Thread类:创建一个新的类,该类继承自Thread类,然后重写...

    java多线程实现大批量数据导入源码

    Java通过`Thread`类来创建和管理线程。每个线程都包含一个独立的执行路径,它们可以同时运行,以实现任务的并发处理。在处理大数据时,我们将数据划分为多个部分,每个部分由一个单独的线程来处理,这样可以显著减少...

    Java 多线程 PPT

    创建Java线程主要有两种方式: 1. 继承Thread类:创建Thread的子类并重写run()方法,然后通过调用start()方法启动线程。 2. 实现Runnable接口:创建一个实现了Runnable接口的类,实现run()方法,然后将该类的实例...

    java多线程处理数据库数据

    在Java中,我们可以使用`Thread`类或者`Runnable`接口来创建线程。然而,为了更好地管理和控制线程,Java并发包提供了如`ExecutorService`、`Future`、`Callable`等高级接口和类,它们简化了多线程编程,并提供了更...

    JAVA多线程练习题答案。

    在本文中,我们将对 JAVA 多线程练习题的答案进行详细的解释和分析。这些题目涵盖了 JAVA 多线程编程的基本概念和技术,包括线程的生命周期、线程同步、线程状态、线程优先级、线程安全等方面。 一、单项选择题 在...

    java多线程分页查询

    在Java中,可以通过继承`Thread`类或者实现`Runnable`接口来创建线程。 ##### 3. 多线程分页查询方案设计 针对数据量较大的场景,本文介绍的多线程分页查询方案主要包含以下几个步骤: - **步骤1:计算总记录数**...

    汪文君JAVA多线程编程实战(完整不加密)

    在Java平台中,多线程是并发处理的基础,对于构建高效、可扩展的系统至关重要。 Java多线程允许程序同时执行多个独立的代码段,这在处理大数据、网络通信、用户界面更新等场景中尤其有用。书中详细介绍了Java多线程...

Global site tag (gtag.js) - Google Analytics