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并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
随机推荐
- Spring的注解问题
Annotation(注解)概述 从JDK5.0开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation(注解). Annotation其实就是代码里的特殊标记,它用于替 ...
- MyBatis一对多和多对多xml配置
MyBatis一对多和多对多xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ma ...
- 微信小程序post 服务端无法获得参数问题
header中需要改为 "Content-Type": "application/x-www-form-urlencoded"
- Spring cloud搭建Eureka高可用注册中心
注册中心在微服务中是必不可少的一部分,主要用来实现服务自治的功能,本文则主要记载使用Netflix提供的Eureka作为注册中心,来实现服务自治的功能. 实际上Eureka的集群搭建方法很简单:每一台 ...
- 使用RabbitMQ做数据接收和处理时,自动关闭
场景:N个客户端向MQ里发送数据:服务器上有另一个控制台程序(假设叫ServerClient)来处理这里数据(往数据库保存).方向为Client * n→MQSERVER→ServerClient 问 ...
- Hadoop学习(3)-mapreduce快速入门加yarn的安装
mapreduce是一个运算框架,让多台机器进行并行进行运算, 他把所有的计算都分为两个阶段,一个是map阶段,一个是reduce阶段 map阶段:读取hdfs中的文件,分给多个机器上的maptask ...
- python字符编码-文件操作
字符编码 字符编码历史及发展 为什么有字符编码 ''' 原因:人们想要将数据存入计算机 计算机的能存储的信息都是二进制的数据 内存是基于电工作的,而电信号只有高低频两种,就用01来表示高低电频,所以计 ...
- 简单web网页与SSM后台交互
简单web网页与SSM后台交互 情况说明 如今,已经搭建好SSM后台开发环境,并且可以经由postman工具测试成功.现在尝试写出web前端网页,通过实现简单的提交.注册.查询功能来加深对前后端数据传 ...
- 用python输出菱形
num = eval(input('请输入最多*所在行数:')) a = num b = num #上三角 for i in range(1,num+1): print((a-1) *' ', (2* ...
- [ PyQt入门教程 ] PyQt5环境搭建和配置
PyQt入门系列教程主要目的是希望通过该系列课程学习,可以使用PyQt5工具快速实现简单的界面开发,包括界面设计.布局管理以及业务逻辑实现(信号与槽).简单说就是可以使用PyQt5工具快速画一个控件摆 ...