java学习之- 线程继承Thread类
标签(空格分隔): 线程
在java。lang包中有个Thread子类,大家可以自行查阅文档,及范例;
如何在自定义的代码中,自定义一个线程呢?
1.通过对api的查找,java已经提供了对线程这类事物的描述,就是Thread类,创建线程的第一种方式,就是继承Thread类:
2.public void run(),如果该线程是使用独立的Runnable运行对象的run方法,否则该方法不执行任何操作并返回,Thread的子类应该重写该方法;
3.查看API的时候:查看start方法,使用该线程开始执行,java虚拟机调用该线程的run方法;
class Demo extends Thread{
pulbic void run(){
System.out.println("Demo run");
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d=new Demo();
d.start();
}
}
上述:
创建一个子类的步骤如下:
1.定义类继承Thread;
2.复写Thread的run方法;
3.调用线程的start方法,该方法有两个作用,一个启动线程,二调用run方法;
4.new一个对象的时候只是创建了一个线程,并未执行,只有调用了start的方法才是被执行了;
如下代码的分析
package com.wangaling;
class Demo extends Thread{
public void run(){
for(int x=0;x<60;x++) {
System.out.println("demo run-----"+x);
}
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d= new Demo();//创建好一个线程
d.start();
for(int x=0;x<60;x++){
System.out.println("helloworld!----"+x);
}
}
}
执行结果:
demo run-----0
helloworld!----0
demo run-----1
helloworld!----1
demo run-----2
helloworld!----2
demo run-----3
helloworld!----3
helloworld!----4
helloworld!----5
helloworld!----6
demo run-----4
helloworld!----7
demo run-----5
helloworld!----8
demo run-----6
helloworld!----9
demo run-----7
helloworld!----10
demo run-----8
helloworld!----11
demo run-----9
demo run-----10
helloworld!----12
demo run-----11
helloworld!----13
helloworld!----14
demo run-----12
helloworld!----15
demo run-----13
helloworld!----16
demo run-----14
helloworld!----17
demo run-----15
helloworld!----18
demo run-----16
helloworld!----19
demo run-----17
helloworld!----20
helloworld!----21
demo run-----18
helloworld!----22
demo run-----19
helloworld!----23
demo run-----20
helloworld!----24
demo run-----21
helloworld!----25
demo run-----22
helloworld!----26
demo run-----23
helloworld!----27
helloworld!----28
helloworld!----29
helloworld!----30
helloworld!----31
demo run-----24
helloworld!----32
demo run-----25
helloworld!----33
helloworld!----34
demo run-----26
helloworld!----35
demo run-----27
helloworld!----36
demo run-----28
helloworld!----37
helloworld!----38
demo run-----29
helloworld!----39
demo run-----30
helloworld!----40
demo run-----31
demo run-----32
demo run-----33
demo run-----34
demo run-----35
demo run-----36
demo run-----37
demo run-----38
helloworld!----41
demo run-----39
helloworld!----42
demo run-----40
helloworld!----43
demo run-----41
helloworld!----44
demo run-----42
helloworld!----45
helloworld!----46
helloworld!----47
helloworld!----48
helloworld!----49
demo run-----43
helloworld!----50
helloworld!----51
demo run-----44
helloworld!----52
demo run-----45
helloworld!----53
helloworld!----54
helloworld!----55
helloworld!----56
demo run-----46
helloworld!----57
demo run-----47
helloworld!----58
demo run-----48
demo run-----49
demo run-----50
demo run-----51
demo run-----52
demo run-----53
demo run-----54
helloworld!----59
demo run-----55
demo run-----56
demo run-----57
demo run-----58
demo run-----59
Process finished with exit code 0
上述结果产生的分析:
1.main----->d=new demo()产生了一个新的控制单元---->start()开启新的控制单,执行:demo run;
2.main----->d=new demo()---->start()-->新的控制单元,同时朱函数还要继续执行:helloworld;
3.以上就开启一个多线程;
4.ThreadDemo开启了2个执行路径:一个是main,一个是d,那么两个会同时执行吗?其实是不可能的,Windows是多任务操作系统,你看着电视听这音乐,看着是同时执行,其实不是的,执行以下电视,执行以下音乐,他在两个任务之间做了快速切换,你要知道CPU切换的是进程里面的线程,所以到这里大家就会明白为啥任务开的多,反而越慢;
5.目前我们的例子是:一个进程里面有多个线程,多个线程抢夺CPU资源,谁抢到CPU的资源,谁就执行,通过打印看到效果;
6.电脑实现双核,是不是CPU就可以实现了同时执行,双核之后内存就是瓶颈;
7.发现运行结果每次都不同:因为多个线程都在获取CPU的执行使用权,CPU执行到谁,谁就运行明确一点,在某一个时刻,只能有一个程序在运行,当然多核除外,CPU在做着快速的切换,已达到看上去同时执行的效果,我们可以形象的把多线程的运行形容为在互相抢夺CPU的资源或者称之为执行权,这就是多线程的一个特性,叫随机性,谁抢到谁执行,至于执行多长时间目前是CPU说的算;
java学习之- 线程继承Thread类的更多相关文章
- Java之同步方法处理继承Thread类的线程安全问题
/** * 使用同步方法处理继承Thread类的方式中的线程安全问题 * */class Window4 extends Thread { private static int ticket = 10 ...
- Java中实现多线程继承Thread类与实现Runnable接口的区别
Java中线程的创建有两种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 在实际应用中, ...
- java多线程(一)之继承Thread类
一.概述 进程:正在执行的应用程序 线程:进程的执行单元,执行路径 单线程:一个应用程序只有一条执行路径 多线程:一个应用程序有多条执行路径 二.两种实现方式, 下面为第一种方式: 继承Thread类 ...
- Java多线程实现......(1,继承Thread类)
MyThread.java 中的代码: public class MyThread extends Thread{ private int startPrint,printCount; private ...
- JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)
实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...
- Android(java)学习笔记62:继承Thread类创建线程类
package cn.itcast_02; /* * 该类要重写run()方法,为什么呢? * 不是类中的所有代码都需要被线程执行的. * 而这个时候,为了区分哪些代码能够被线程执行,java提供了T ...
- Android(java)学习笔记2:继承Thread类创建线程类
1. 继承Thread类 创建线程类: package cn.itcast_02; /* * 该类要重写run()方法,为什么呢? * 不是类中的所有代码都需要被线程执行的. * 而这个时候,为了区分 ...
- 【转载】JAVA中线程的两种实现方法-实现Runnable接口和继承Thread类
转自: http://blog.csdn.net/sunguangran/article/details/6069317 非常感谢原作者,整理的这么详细. 在java中可有两种方式实现多线程,一种是继 ...
- Java线程演示样例 - 继承Thread类和实现Runnable接口
进程(Process)和线程(Thread)是程序执行的两个基本单元. Java并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
随机推荐
- 十、SQL中EXISTS的用法 十三、sql server not exists
十.SQL中EXISTS的用法 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False EXISTS 指定一个子查询,检测 行 的存在. 语法 ...
- 【剑指offer】面试题(三)
package com.haxianhe.test; /** *题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序, *每一列都按照从上到下递增的顺序排序. *请完成一个函数, *输入这样的一 ...
- python课堂整理12---递归
一.递归特性 1.必须有一个明确的结束条件 2.每次进入更深一层递归时,问题规模相比上次递归都应有所减少 3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据 ...
- Spring Cloud 之 Gateway.
一.Gateway 和 Zuul 的区别 Zuul 基于servlet 2.5 (works with 3.x),使用阻塞API.它不支持任何长期的连接,如websocket. Gateway建立在S ...
- IO流1
一.I/0:input/output1.java.io.file表示:文件或文件夹(目录)File f = new File("文件路径");注意:相对路径:非web项目的相对都是 ...
- Golang高效实践之泛谈篇
前言 我博客之前的Golang高效实践系列博客中已经系统的介绍了Golang的一些高效实践建议,例如: <Golang高效实践之interface.reflection.json实践>&l ...
- 关于STM32F103+ESP8266+阿里云过程之设备状态更新至阿里云(三)
设备与阿里云完成发布订阅的功能,接下来就是将设备状态如温湿度,PM2.5值上报更新至阿里云. 1.查看Topic. 在阿里云平台上 设备->Topic列表中查看.在产品中也可以看到对应的Topi ...
- javaweb入门-----request与response的作用
request对象和request对象的原理 1.request和response对象request对象和request对象的原理时由服务器创建的,我们来使用它们 2.request对象是来获取请求消 ...
- 【Maven】Mac 使用 zsh 后 mvn 命令就无效
RT -- 解决方法: 打开 .zshrc 文件,将 Maven 环境变量配置加入其中,或者 将 source ~/.bash_profile 添加到 .zshrc 中. PS: 之前搞不懂,每次使用 ...
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...