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并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
随机推荐
- Netty中的策略者模式
策略者模式的特点 在设计类的继承体系时,我们会刻意的把公共的部分都提取到基类中 比如先设计Person类,把人类都具有的行为放到这个Person,特有的行为设计成抽象方法,让子类具体去实现, 这样后续 ...
- 在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core
在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core 一.安装配置MiniProfiler 在现有的ASP.NET Core MVC WebApi 项目 ...
- 《JSP数据交互总结》
1.1.1为什么需要动态网页 静态网页的内容是固定的,不能提供个性化和定制化的服务,因此,动态网页技术逐渐发展起来. 1.1.2什么是动态页面 动态网页是指在服务器端运行的使用程序语言设计的交互式网页 ...
- C# sql 批量插入数据库的语句
//执行DataTable数据导入 public static int UpdateDt(string strConn, DataTable dt) { try { string tablaName ...
- 极简代码神器:Lombok使用教程
Lombok 是一个非常神奇的 java 类库,会利用注解自动生成 java Bean 中烦人的 Getter.Setter,还能自动生成 logger.ToString.HashCode.Build ...
- BME200加密网关,在电力与工业应用的加密网关设计与介绍
加密通信网关,顾名思义就是带加密的通信网关终端, 一般业内主是需用到是工业通信关行业的为主的.,BME200加密通信网关,主要电力和工业互联网相关领域开发的一款加密通信网关. 为什么出现加密网关 1 ...
- Ubuntu中修改默认开机项
1首先,按住Ctrl+Alt+t打开终端 2输入cd /etc/default 3输入sudo sudo nano grub 并按照提示输入密码 4在我们开机的时候,可以看到自己想要默认的开机项是多少 ...
- 数据结构-二叉搜索树和二叉树排序算法(python实现)
今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...
- java高并发系列 - 第22天:java中底层工具类Unsafe,高手必须要了解
这是java高并发系列第22篇文章,文章基于jdk1.8环境. 本文主要内容 基本介绍. 通过反射获取Unsafe实例 Unsafe中的CAS操作 Unsafe中原子操作相关方法介绍 Unsafe中线 ...
- ASP.NET Web项目发布选项:“允许更新此预编译站点” 详解
目录 #使用visual studio 发布web项目 #"允许更新此预编译站点" 选项的意义 1.选中 "允许更新此预编译站点" 2.不选中 "允许 ...