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并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
随机推荐
- 【Java中级】(一)面向对象的特性与八种基本类型
1.1.Java 基本数据类型: Java提供了8种基本类型.六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. byte.short.int.long.float.double. ...
- Spring Boot2(十五):Shiro记住我rememberMe、验证码Kaptcha
接着上次学习的<Spring Boot2(十二):手摸手教你搭建Shiro安全框架>,实现了Shiro的认证和授权.今天继续在这个基础上学习Shiro实现功能记住我rememberMe,以 ...
- 搭建python环境
参考文章:https://blog.csdn.net/qq_33855133/article/details/73106176 对于配置环境变量,懂些技术的人来说,都是很简单. 变量是在操作系统中一个 ...
- 最短代码实现包含100个key的字典,且每个value值不同
最短代码实现包含100个key的字典,且每个value值不同 {x:x*2 for x in range(100)}
- Office2010安装问题锦集
问题一,每次打开office 2010,都会出现重新配置的对话框. 解决姿势: 大部分搜索到的解决方法大概有2种, 一个是修改注册表,在注册表中这个这个位置增加一个名为NoRereg的32位World ...
- Windows 使用 helm3 和 kubectl
简介: 主要原因是,我不会 vim ,在 linux 上修改 charts 的很蹩脚,所以就想着能不能再 windows 上执行 helm 命令,将 charts install linux 上搭建的 ...
- case和decode的用法(行转列)
创建了一张成绩表,如下图所示: 在oracle中,这两个函数我们都可以使用,代码及结果如下: decode用法: select Name,decode(Subject,'语文',1,'数学',2,'英 ...
- TCP加速机制是如何加速的?
一.什么是TCP加速? TCP加速就是在高时延链路提高吞吐量的一系列解决方案. 二.为什么需要对TCP进行加速? 1.传统的TCP拥塞控制算法并不适用于高时延.高误码的链路. 2.随着we ...
- AI中台——智能聊天机器人平台的架构与应用(分享实录)
内容来源:宜信技术学院第3期技术沙龙-线上直播|AI中台——智能聊天机器人平台 主讲人:宜信科技中心AI中台团队负责人王东 导读:随着“中台”战略的提出,目前宜信中台建设在思想理念及架构设计上都已经取 ...
- Linux 常见的常识及常用快捷键方式
1. ,请写出linux系统中常见一级目录的名称及作用. /root : 超级用户的家目录 /home: 普通用户的家目录 /boot: 启动目录,启动相关文件(系统内核启动文件) /de ...