1、java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建、(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使用CPU,此线程就会脱离创建他的主线程开始自己使命。此线程调用start()方法。通知JVM,这样JVM就会知道一个新的线程排队等候了。子类线程中必须重写Thread父类中的run方法,不然会发生异常。)。线中断机制,就是此线程使用Thread中的方法 sleep(int millsecond)此时线程会方法中断。经过millsecond后继续到CPU中排队等候。线程使用wait()方法。发生等待,等待状态的线程不会主动进入CPU等待序列,除非其他线程调用 notify()方法唤醒,使得他重新进入到CPU的等待序列,从中断处继续运行。线程死亡(所谓线程死亡就是该线程放弃了线程对象的内存)发生线程死亡的原因主要有两个,一是:线程运行完毕,结束了线程的run()方法。另一个原因是:线程被强制终止。

public class BinFa {

public static void main(String[] args) {

// TODO Auto-generated method stub

SpeakElephent elephent;

SpeakCar car;

elephent=new SpeakElephent();

car=new SpeakCar();

elephent.start();

car.start();

for(int i=0;i<=10;i++){

System.out.println("主人"+i+" ");

}

}

}

interface ShuZu{

String a[]={"花花","丽丽","小洁","明明","亚明"};

void Test( );

}

class SpeakElephent  extends Thread implements ShuZu{

public void run(){

for(String i:a){

System.out.println("大象"+i);

}

}

public void Test(){

System.out.println("这是线程大象");

}

}

class SpeakCar extends Thread implements ShuZu{

public void run(){

for(String i:a){

System.out.println("汽车"+i);

}

}

public void Test(){

System.out.println("这是线程汽车");

}

}

2、线程调度和优先级问题

在线程机制中,调整线程的优先级还可以通过方法 setPriority(int grade )方法设置线程的优先级。但是在实际的的设置中应该假设,线会随时被剥夺CPU的使用权限,不能仅靠设置线程的优先权决定查询的正确执行。JVM中存在线程的管理机制。负责管理线程的调度顺序。

3、Thread和Runnable区别,启动main的时候,java虚拟机启动一个进程,主进程main在main()的调用的时候创建,但是start()方法调用后并不是立即执行多线程代码,而是使得该线程变成了可运行态(runnable),至于什么时候运行是由系统决定的。其中Thread.sleep()方法的调用只是为了不让抢钱的线程不单独霸占CPU资源。留出一定的时间非其他的线程。如果一个类继承Thread,则不适合资源共享,但是如果实现了Runnable接口的话,实现资源共享就很容易。,想要创建一个线程可运行实例,需要实现Runnable接口或者继承Thread类,Runnable只有一个抽象的Run()方法。可以这样说java中的线程只是操作系统中的的一个映射。java中的线程运行效率不可能高于底层语言的效率。这是因为java中的线程的创建和调用都要进过JVM,JVM再向下调用。

这里附上java.lang.Runnable源码

java.lang

Interface Runnable

public interface Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Threadmethods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Since:JDK1.0See Also:

Method Summary

void

Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Method Detail

run

void run()

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

The general contract of the method run is that it may take any action whatsoever.

public class XianChengTongBu {

public static void main(String[] args) {

// TODO Auto-generated method stub

EleTarget t=new EleTarget();

new Thread(t).start();

new Thread(t).start();

new Thread(t).start();

new Thread(t).start();

}

}

class EleTarget implements Runnable{

private  int ticket=10;

public  synchronized void  run(){

for(int i=0;i<=10;i++){

if(this.ticket>0){

System.out.println("买票"+this.ticket--);

}

}

}

}

4、Runnable方法实现线程机制

public class GongXiangDanYan {

public static void main(String[] args) {

// TODO Auto-generated method stub

House house=new House();

house.setWater(10);

Thread dog,cat;

dog=new Thread(house);

cat=new Thread(house);

dog.setName("Dog");

cat.setName("Cat");

dog.start();

cat.start();

}

}

class House implements Runnable{

int wateAmount;

public void setWater(int w){

wateAmount=w;

}

public void run(){

while(wateAmount>=0){

String name=Thread.currentThread().getName();//得到当前线程的对象名字

if(name.equals("Dog")){

System.out.println(name+"Drink");

wateAmount=wateAmount-2;

}else if(name.equals("Cat")){

System.out.println(name+"Drink");

wateAmount=wateAmount-1;

}

System.out.println("left"+wateAmount);

try{

Thread.sleep(2000);

}catch(InterruptedException ex){

if(wateAmount<=0){

return;

}

}

}

}

}

5、线程中常见的方法,start(),run(), sleep(int millsecond) ,isAlive(), currentThread(), interrupt()

public class CurrentThread {

public static void main(String[] args) {

// TODO Auto-generated method stub

Home home=new Home();

Thread showTime=new Thread(home);

showTime.start();

}

}

class Home implements Runnable{

int time;

SimpleDateFormat m=new SimpleDateFormat("hh:mm:ss");

Date date;

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

date=new Date();

System.out.println(m.format(date));

time++;

try{

Thread.sleep(1000);

}catch(InterruptedException ex){

if(time==4){

Thread thread=Thread.currentThread();

thread=new Thread(this);

thread.start();

}

}

}

}

}

interrupt()方法

public class ThreadGuanXi {

public static void main(String[] args) {

// TODO Auto-generated method stub

ClassRoom room=new ClassRoom();

room.student.start();

room.teacher.start();

}

}

class ClassRoom implements Runnable{

Thread student,teacher;

ClassRoom(){

teacher=new Thread(this);

student=new Thread(this);

teacher.setName("冯涛");

student.setName("陈武阳");

}

public void run(){

if(Thread.currentThread()==student){

try{

System.out.println(student.getName()+"***********在睡觉************");

Thread.sleep(1000*60);

}catch(InterruptedException ex){

System.out.println("被"+teacher.getName()+"老师叫醒啦");

}

}

else if(Thread.currentThread()==teacher){

for(int i=0;i<3;i++){

System.out.println("###################上课#########");

try{

Thread.sleep(500);

}catch(InterruptedException ex){

}

student.interrupt();

}

}

}

}

6、线程同步问题,多个线程调用synchronize必须遵循同步机制

java中activeThread_java多线程机制中的Thread和Runnable()区别相关推荐

  1. Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...

  2. java中实现多线程的三种方式

    java中实现多线程的三种方式 1.实现多线程的方法: 在java中实现多线程的两途径:继承Thread类,实现Runable接口(Callable) 2.继承Thread类实现多线程: ​ 继承类T ...

  3. Java中的多线程编程(超详细总结)

    文章目录 Java中的多线程编程(超详细总结) 一.线程与多线程的概念 二.线程与进程之间的关系 三.一个线程的生命周期 四.多线程的目的和意义 五.线程的实现的方式 Java中的多线程编程(超详细总 ...

  4. JAVA中的多线程(一)

    JAVA中的多线程(一) 进程:是一个正在执行中的程序 每一个进程执行都有一个执行的顺序,该顺序是一个执行路径,或者叫控制单元 线程:就是进程中的一个独立的控制单元 线程在控制着进程的执行 一个进程中 ...

  5. java中的多线程有什么意义_Java多线程与并发面试题(小结)

    1,什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完成一 ...

  6. JAVA中的多线程(八):线程的优先级和yield方法

    JAVA中的多线程(八):线程的优先级和yield方法 优先级代表着抢资源的频率 所有线程默认优先级是5 yield()临时释放线程的执行权 1 class Demo implements Runna ...

  7. JAVA中的多线程与运动仿真(1)——用JAVA来放一场烟花

    JAVA中的多线程与运动仿真(1)--用JAVA来放一场烟花 一.实现效果的简单展示: 初步实现的动态效果为在鼠标点击之后,点击之处出现一簇小球,然后向不同方向散开变大. 利用这一效果,再在后续增加颜 ...

  8. 草根方式学习java中的多线程

    草根方式学习java中的多线程 下面有具体的代码和截图 源码点这里 多线程即在同一时间,可以做多件事情(说白了,就是齐头并进) 单线程就是按部就班 创建多线程有2种方式,分别是继承线程Thread类, ...

  9. Java中的多线程基本介绍

    在 Java 中,多线程是指同时执行两个或多个线程以最大限度地利用 CPU 的过程. Java 中的线程是一个轻量级进程,只需要较少的资源即可创建和共享进程资源. 多线程和多进程用于 Java 中的多 ...

  10. Java基础——深入理解Java中的多线程(超级详细,值得你看)

    Java中的多线程 进程(process)是程序的一次执行过程,或是正在运行的有一个程序,或是正在运行的一个程序.是一个动态的过程:有它自身的产生.存在和消亡的过程.--生命周期. 线程(thread ...

最新文章

  1. R语言nrow函数获取dataframe或者matrix行计数统计
  2. 全面讲解电脑主板-图文
  3. 新一轮电信业改革的两条路
  4. C++【“using namespace std”的意思、#include “iostream“与#include < iostream>区别、< iostream>与< iostream.h>区别】
  5. 推荐一位零基础学 NLP 的大佬,内含成长历程
  6. 马斯克揭晓谜底!SpaceX大火箭将送日本富豪环月七日游
  7. python解释器的使用
  8. 微信小程序的民宿客房预订uniapp小程序
  9. LintCode 171. Anagrams
  10. 《系统分析与设计》课程设计——医院门诊信息管理查询系统
  11. 小程序 实现手写签名功能
  12. python、pygame开发的太空大战游戏源代码
  13. 8脚 tja1050t_高速光耦:CAN总线通信硬件原理图(采用TJA1050T CAN总线驱
  14. APP开发者应办理许可或备案手续
  15. 基于Flask快速搭建一个管理系统
  16. 海报设计PSD模板——抖音Glitch故障艺术风格
  17. python使用statsmodels包中的tsa.acf函数计算时间序列数据所有滞后位置个数(级别)的自相关性、tsaplots函数可视化时间序列数据所有滞后位置个数(级别)的自相关性
  18. 苹果iOS app上架流程
  19. PMBOK(第五版)学习笔记 —— 5 项目范围管理
  20. 调节e18-d80nk的测量距离_常用测量仪器操作教学 | 视频+图文

热门文章

  1. 配置香橙派zeropuls2
  2. 虚拟串口VSPD和XCOM的下载+安装+使用
  3. OpenCV 角度计算
  4. 宝塔远程桌面助手linux密码不知道,宝塔远程桌面助手的教程
  5. mysql workbench中PK,NN,UQ,BIN,UN,ZF,AI字段类型标识说明
  6. 使用 SHAP库 对模型预测作解释
  7. widows升级nodejs版本
  8. Flink Event Time , Processing Time 和 Ingestion Time概念
  9. 机器学习(四):决策树绘画(基础篇)
  10. mmo服务器 性能测试,【Zinx应用-MMO游戏案例-(5)构建项目及用户上线】Golang轻量级并发服务器框架...