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并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
随机推荐
- [leetcode] 45. Jump Game II(hard)
原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...
- 如何在windows上玩转redis的最新特性?
想要了解redis的最新特性,可是windows下的可以安装的版本最高为3.2,想要验证redis的诸如stream特性的话,就无能为力了. 解决方法之一在windows上安装虚拟机,然后再虚拟机上安 ...
- Anacodna之conda的使用
yum install -y bunzip2 wget https://repo.continuum.io/archive/Anaconda2-5.0.1-Linux-x86_64.sh chmod ...
- zookeeper集群搭建及常用场景实现
本文完整源码地址 基于zookeeper的常用用法.分布式锁.分布式队列及leader选举实现 https://github.com/killianxu/zookeeper_example zooke ...
- 彻底搞懂Python切片操作
在利用Python解决各种实际问题的过程中,经常会遇到从某个对象中抽取部分值的情况,切片操作正是专门用于完成这一操作的有力武器.理论上而言,只要条件表达式得当,可以通过单次或多次切片操作实现任 ...
- 【Intellij IDEA】设置 jdk 版本
File -> Project Structure... -> Project,如图所示:
- springboot+kafka+邮件发送(最佳实践)
导读 集成spring-kafka,生产者生产邮件message,消费者负责发送 引入线程池,多线程发送消息 多邮件服务器配置 定时任务生产消息:计划邮件发送 实现过程 导入依赖 <proper ...
- Task CancellationTokenSource和Task.WhenAll的应用
Task是.net4.0推出的异步编程类,与ThreadPool.QueneUserWorkItem方法类似的是,Task也是使用线程池来工作的.但Task比起这个QueneUserWorkItem的 ...
- 深入理解JVM-类加载器深入解析(1)
类加载 在java代码中,类型的加载,连接与初始化过程都是在程序运行期间完成的 类型:表示的Object本身,并不是指一个对象,也就是class. 运行期间:表示的是一种runtime的概念,在运行期 ...
- [ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框
本文主要介绍PyQt5界面最基本使用的单选按钮.复选框.下拉框三种控件的使用方法进行介绍. 1.RadioButton单选按钮/CheckBox复选框.需要知道如何判断单选按钮是否被选中. 2.Com ...