1.线程名的占用问题
public class Demo1 extends Thread{
private String who;
public void run()
{
System.out.println(who + ":" + this.getName());
}
public Demo1(String who)
{
super();
this.who = who;
}
public Demo1(String who, String name)
{
super(name);
this.who = who;
}
public static void main(String[] args)
{
Demo1 thread1 = new Demo1 ("thread1", "MyThread1");
Demo1 thread2 = new Demo1 ("thread2","MyThread2");
Demo1 thread3 = new Demo1 ("thread3");
thread1.start();
thread2.start();
thread3.start();
}
}
结果:
thread1:MyThread1
thread3:Thread-0
thread2:MyThread2
注:输出结果顺序不固定
public class Demo1 extends Thread{
private String who;
public void run()
{
System.out.println(who + ":" + this.getName());
}
public Demo1(String who)
{
super();
this.who = who;
}
public Demo1(String who, String name)
{
super(name);
this.who = who;
}
public static void main(String[] args)
{
Demo1 thread1 = new Demo1 ("thread1", "MyThread1");
Demo1 thread2 = new Demo1 ("thread2");
Demo1 thread3 = new Demo1 ("thread3");
thread2.setName("MyThread2");
thread1.start();
thread2.start();
thread3.start();
}
}
thread2:MyThread2
thread3:Thread-1
thread1:MyThread1
通过比较:创建线程时,如果不设置线程名,系统会自动分配线程名,
并占位,通过thread3:Thread-1就可断定thread2占据着Thread-0的位置。
2.Thread类的start方法不能多次调用,如不能调用两次thread1.start()方法。否则会抛出一个IllegalThreadStateException异常
3.如果通过继承Runnable的方式创建线程,很多方法都不能用(如isAlive等)
Runnable接口源码如下:
/**
* The <code>Runnable</code> 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 <code>run</code>.
* <p>
* This interface is designed to provide a common protocol for objects that
* wish to execute code while they are active. For example,
* <code>Runnable</code> is implemented by class <code>Thread</code>.
* Being active simply means that a thread has been started and has not
* yet been stopped.
* <p>
* In addition, <code>Runnable</code> provides the means for a class to be
* active while not subclassing <code>Thread</code>. A class that implements
* <code>Runnable</code> can run without subclassing <code>Thread</code>
* by instantiating a <code>Thread</code> instance and passing itself in
* as the target. In most cases, the <code>Runnable</code> interface should
* be used if you are only planning to override the <code>run()</code>
* method and no other <code>Thread</code> methods.
* This is important because classes should not be subclassed
* unless the programmer intends on modifying or enhancing the fundamental
* behavior of the class.
*
* @author Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0
*/
public
interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
问题:如果我需要extends其他类,又要用到isAlive方法怎么办?
分享到:
相关推荐
《JAVA多线程设计模式》PDF 下载 《Java线程 高清晰中文第二版》中文第二版(PDF) 前言 第一章 线程简介 Java术语 线程概述 为什么要使用线程? 总结 第二章 Java线程API 通过Thread类创建线程 使用Runable接口...
电子书相关:包含4个有关JAVA线程的电子书(几乎涵盖全部有关线程的书籍) OReilly.Java.Threads.3rd.Edition.Sep.2004.eBook-DDU Java Thread Programming (Sams) java线程第二版中英文 java线程第二版中英文 ...
java socket 多线程 简易的实现方法 细节问题没有处理 适合新手
Java多线程初学者指南,共12篇文档,由浅入深,举例说明,包括一些疑点都解释得很清楚,类,方法,属性同步的区别等细节也都介绍得很全面,是个很好的学习资料。
不同的平台,内存模型是不一样的,但是jvm的内存模型规范是统一的。...JVM定义了自己的内存模型,屏蔽了底层平台内存管理细节,对于java开发人员,要清楚在jvm内存模型的基础上,如果解决多线程的可见性和有序性。
不同的平台,内存模型是不一样的,但是jvm的内存模型规范是统一的。...JVM定义了自己的内存模型,屏蔽了底层平台内存管理细节,对于java开发人员,要清楚在jvm模型的基础上,如果解决多线程的可见性和有序性。
此资源集包含了一套完整的Java实现的多线程与线程安全实践项目源代码、相关的毕业论文以及详尽的使用说明。它旨在提供一个全面、深入的学习和研究工具,适用于本科课程设计、毕业设计以及任何希望深入学习Java编程的...
主要介绍了Java 多线程使用要点分析的相关资料,Java 多线程开发需要主要的细节问题很多,这里就说下,需要的朋友可以参考下
java笔记第二部分_面向对象以及细节,该笔记是自己通过观看毕向东老师视频记录的。
1、面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: ...总共90多道题目,包含面向对象、算法、多线程等面试题及详解 大厂面试题集,纯人工手写,分享不易,有问题敬请谅解 。。。。。。。。
Java多线程 多线程中的常见问题 同步关键字原理 多线程的三大核心 对锁的一些认知 ReentrantLock实现原理 ConcurrentHashMap 的实现原理 如何优雅地使用和理解线程池 深入理解线程通信 一个线程召集的诡异事件 ...
java多线程中所有的知识点概括,剩下具体细节的,就交给强大的百度把。
多线程 深入理解内部类 javac和javap Java8新特性终极指南 序列化和反序列化 继承、封装、多态的实现原理 容器 Java集合类总结 Java集合详解1:一文读懂ArrayList,Vector与Stack使用方法和实现原理 Java集合详解2:...
前言 这个【多线程】的文档是全程手打肛出来的,共129页,希望各位老铁可以转发本文支持一下... 1.6Java实现多线程需要注意的细节 二、Thread类解析 1.Thread线程类API 1.1设置线程名 1.2守护线程 1.3优先级线程 1
第4章从介绍多线程技术带来的好处开始,讲述了如何启动和终止线程以及线程的状态,详细阐述了多线程之间进行通信的基本方式和等待/通知经典范式。 第5章介绍Java并发包中与锁相关的API和组件,以及这些API和组件的...
3. 题目考察点含有( 类,继承,多态,异常,多线程等等)语法知识细节。让知识的体系更加牢固。 注意: 题目内部有代码提示,并附着答案,可以进行私信交流,有需要的可以向我要无答案附着版本。
点量Http、FTP多线程断点续传下载组件(下载DLL)的开发目的是让用户可以无需关心Http/FTP协议的具体细节,只需要几十行甚至几行代码,便可以实现一个功能完善的Http/FTP下载软件。点量Http/FTP下载组件(DLL)支持...
Java是一门很优秀的编程语言,具有面向对象、与平台无关、安全、稳定和多线程等特点,是目前软件设计中极为健壮的编程语言。Java不仅可以用来开发大型的应用程序,而且特别适合于Internet的应用开发。Java确实具备了...
第4章从介绍多线程技术带来的好处开始,讲述了如何启动和终止线程以及线程的状态,详细阐述了多线程之间进行通信的基本方式和等待/通知经典范式。第5章介绍Java并发包中与锁相关的API和组件,以及这些API和组件的...
从理论到实践,详细的阐述了java多线程编程的要点,注重细节和实战