`
浴盆儿
  • 浏览: 80680 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

线程随笔-创建线程的基本方法

    博客分类:
  • Java
阅读更多

The method One:

 

public class testThreadNormal {

	public static void main(String[] args) {
		Runner r1 = new Runner();
		Thread t1 = new Thread(r1);
		t1.start();
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}
}

class Runner implements Runnable {

	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

 

 

 The result of method One:

 

times of calling thread of Runner:1
times of calling main thread1
times of calling thread of Runner:2
times of calling main thread2
times of calling thread of Runner:3
times of calling main thread3
times of calling thread of Runner:4
times of calling main thread4
times of calling thread of Runner:5
times of calling main thread5
times of calling thread of Runner:6
times of calling main thread6
times of calling thread of Runner:7
times of calling main thread7
times of calling thread of Runner:8
times of calling main thread8
times of calling thread of Runner:9
times of calling main thread9
times of calling thread of Runner:10
times of calling main thread10
times of calling thread of Runner:11
times of calling main thread11
times of calling thread of Runner:12
times of calling main thread12
times of calling thread of Runner:13
times of calling main thread13
times of calling thread of Runner:14
times of calling main thread14
times of calling thread of Runner:15
times of calling main thread15
times of calling thread of Runner:16
times of calling main thread16
times of calling thread of Runner:17
times of calling main thread17
times of calling thread of Runner:18
times of calling main thread18
times of calling thread of Runner:19
times of calling main thread19
times of calling thread of Runner:20
times of calling thread of Runner:21
times of calling thread of Runner:22
times of calling thread of Runner:23
times of calling thread of Runner:24
times of calling thread of Runner:25
times of calling thread of Runner:26
times of calling thread of Runner:27
times of calling thread of Runner:28
times of calling main thread20
times of calling thread of Runner:29
times of calling main thread21
times of calling thread of Runner:30
times of calling main thread22
times of calling thread of Runner:31
times of calling main thread23
times of calling main thread24
times of calling main thread25
times of calling main thread26
times of calling main thread27
times of calling main thread28
times of calling main thread29
times of calling main thread30
times of calling thread of Runner:32
times of calling main thread31
times of calling thread of Runner:33
times of calling main thread32
times of calling thread of Runner:34
times of calling thread of Runner:35
times of calling thread of Runner:36
times of calling thread of Runner:37
times of calling thread of Runner:38
times of calling thread of Runner:39
times of calling main thread33
times of calling thread of Runner:40
times of calling main thread34
times of calling thread of Runner:41
times of calling main thread35
times of calling thread of Runner:42
times of calling main thread36
times of calling thread of Runner:43
times of calling main thread37
times of calling thread of Runner:44
times of calling main thread38
times of calling thread of Runner:45
times of calling main thread39
times of calling thread of Runner:46
times of calling main thread40
times of calling thread of Runner:47
times of calling main thread41
times of calling thread of Runner:48
times of calling main thread42
times of calling thread of Runner:49
times of calling main thread43
times of calling main thread44
times of calling main thread45
times of calling main thread46
times of calling main thread47
times of calling main thread48
times of calling main thread49

 

Pay Attention: a wrong method of constructing a new thread

public class testThread1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Runner1 r1 = new Runner1();
		r1.run();
		for (int i = 1; i < 20; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}

}

class Runner1 implements Runnable {

	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 20; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

The result is below:

we can find than it's excute the method not construct a new thread

times of calling thread of Runner:1
times of calling thread of Runner:2
times of calling thread of Runner:3
times of calling thread of Runner:4
times of calling thread of Runner:5
times of calling thread of Runner:6
times of calling thread of Runner:7
times of calling thread of Runner:8
times of calling thread of Runner:9
times of calling thread of Runner:10
times of calling thread of Runner:11
times of calling thread of Runner:12
times of calling thread of Runner:13
times of calling thread of Runner:14
times of calling thread of Runner:15
times of calling thread of Runner:16
times of calling thread of Runner:17
times of calling thread of Runner:18
times of calling thread of Runner:19
times of calling main thread1
times of calling main thread2
times of calling main thread3
times of calling main thread4
times of calling main thread5
times of calling main thread6
times of calling main thread7
times of calling main thread8
times of calling main thread9
times of calling main thread10
times of calling main thread11
times of calling main thread12
times of calling main thread13
times of calling main thread14
times of calling main thread15
times of calling main thread16
times of calling main thread17
times of calling main thread18
times of calling main thread19

 

The method Two:

 

public class testThread2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Runner3 r1 = new Runner3();
		r1.start();
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}
}

class Runner3 extends Thread {
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

 

the result is similar with the method one

分享到:
评论

相关推荐

    VC随笔线程同步[借鉴].pdf

    总之,VC随笔线程同步的内容涵盖了线程同步的基本概念、需求以及Windows中常见的同步对象及其用法,这些都是开发多线程程序时不可或缺的知识点。正确理解和使用这些同步机制,可以有效地避免并发访问带来的问题,...

    java多线程相关

    本篇将深入探讨Java多线程的相关知识,结合《java随笔杂记.doc》文档中的内容,我们将覆盖以下几个方面: 1. **线程的创建方式** - 实现`Runnable`接口:创建一个实现了`Runnable`接口的类,然后将其实例传递给`...

    WPF开发随笔收录-心电图曲线绘制.doc

    WPF开发随笔收录-心电图曲线绘制 WPF开发随笔收录-心电图曲线绘制是一个关于WPF开发的心电图曲线绘制技术的记录,涵盖了心电图曲线绘制的实现方式、数据接收和绘制功能的实现、自动缩放功能的实现等知识点。 一、...

    2014210-2014307笔记随笔

    标题“2014210-2014307笔记随笔”暗示了这是一份时间跨度从2014年2月10日至3月7日的个人学习记录,可能包含了作者在IT领域的所学所悟,特别是关于编程、软件开发或系统设计的思考。由于描述部分为空,我们无法直接...

    Java完整随笔(学习)

    1. **Java基础**:这部分可能包括了Java的基本语法,如变量、数据类型、运算符、流程控制(if语句、for循环、while循环)、方法定义和调用等。 2. **面向对象编程**:Java的核心特性是面向对象,可能的笔记内容有类...

    并发编程线程安全样例代码.zip

    8. **线程间通信**:`wait()`, `notify()`和`notifyAll()`方法用于线程间的通信,配合`synchronized`实现条件变量。示例可能演示了如何通过这些方法协调线程的执行。 9. **死锁**与**活锁**:死锁是两个或多个线程...

    c++单例模式线程日志类

    在这个特定的场景中,我们讨论的是一个实现了单例模式的日志类,该类专为多线程环境设计,具备日志等级控制、精确的时间戳以及可变长参数和标准格式化输出的功能。 首先,让我们深入了解单例模式。单例模式的主要...

    c++的一些随笔

    通过以上内容的总结,我们可以看到`c++的一些随笔`文档涵盖了C++语言的许多核心特性和高级功能,包括但不限于命名空间、类与对象、模板、智能指针、输入输出流、STL库和多线程等。这些知识点对于理解和掌握C++语言...

    我自己的个人的编程笔记

    - **创建**:通过`new ThreadGroup(groupName)`创建一个新的线程组。其中`groupName`为线程组的名称。 - **优先级设置**:每个线程都可以设置其优先级,使用`setPriority()`方法。线程的优先级决定了线程运行的顺序...

    Android多线程AsyncTask详解

    本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信。 一、android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有其他的应用程序组件在运行时,...

    2021版java高并发常问面试题

    Executor工具类的不同方法按照我们的需求创建了不同的线程池,来满足业务的需求.Executor接口对象能执行我们的线程任务。ExecutorService接口继承了Executor接口并进行了扩展,提供了更多的方法我们能获得任务执行的...

    C#课件 随笔

    2. 基本数据类型:C#包含整型(int, uint, short, long等)、浮点型(float, double)、字符型(char)、布尔型(bool)以及引用类型(如字符串string)。了解它们的存储方式和用法对于编写有效代码至关重要。 3. ...

    Windows-Core-Programming:Windows核心编程随笔,原始码,电子书

    它涵盖了从基本概念到高级技术的广泛主题,包括进程、线程管理、内存分配、I/O操作、设备驱动接口以及系统安全等方面。 在Windows核心编程中,你会学习到: 1. **进程与线程**:了解进程作为资源管理的基本单位,...

    TT随笔二 TInit以及TMap

    本篇随笔“TT随笔二 TInit以及TMap”将探讨这两个主题,聚焦于一个特定的初始化过程(TInit)和一种数据结构(TMap)。 首先,让我们来看看TInit。在很多编程语言中,初始化是一种确保对象或变量在使用前处于预定...

    C#使用后台线程BackgroundWorker处理任务的总结

    可以很好的实现常规操作的同时,还可以及时通知UI,包括当前处理信息和进度等,这个BackgroundWorker的处理在百度里面也是有很多使用的介绍,本篇随笔主要是做一些自己的使用总结,希望也能给读者提供一个参考。...

    Log4j2手册阅读随笔(Log4j2新特性)

    Log4j2的API部分介绍了如何在Java程序中使用Log4j 2进行日志记录的基本方法。开发者可以利用Log4j 2提供的API编写灵活的日志记录语句,以便能够根据不同的日志级别记录信息。 在配置部分,手册详细说明了如何配置...

    Own-learning-note:自己学习前端所阅读的书籍及笔记,框架原始资料,知识点思维导图,项目开发过程整理,随笔等

    4. **异步处理**:JavaScript是单线程的,但通过事件循环和回调函数、Promise、async/await等机制处理异步操作,使得它能在等待IO操作(如网络请求)时执行其他代码,提高了程序的响应性和效率。 除了基础语法,...

    leetcode中国-xinh79.github.io:我的个人博客

    感情随笔 均以过时,写的时候“年代较早” 八月份在学校,可能是审稿审多了,导师作为T-ITS的AE让我成为了一名正式的审稿人。是CCF推荐的 B类学术刊物。 科研其实是现代文明的死角,其最底层的生产逻辑还停滞在手工...

    android倒计时的简单实现

    在Android开发中,倒计时...通过合理利用`onTick()`和`onFinish()`方法,我们可以创建各种各样的倒计时场景,满足应用中的不同需求。在实际项目中,可以根据具体需求对其进行扩展和定制,以实现更加复杂的功能。

Global site tag (gtag.js) - Google Analytics