java多线程入门学习(二)
来源:互联网 发布:编程的工资一般是多少 编辑:程序博客网 时间:2024/05/17 02:31
java多线程入门学习(二)
一.currentThread()和isAlive()
1. currentThread()方法
返回的是当前运行线程,首先来个简单的例子了解一下这个方法的意义:
package cn.sun.method;
public class demo01 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
}
}
返回结果如下:
main
这样我们就知道当前运行的是main主函数。
以下我们创建一个复杂一点的看下这里到底应该是哪个线程在某个时刻运行:
package cn.sun.method;
public class Demo02 extends Thread{
public Demo02(){
System.out.println("demo01 begin...");
System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
System.out.println("this.getName = "+ this.getName());
System.out.println("demo02 end...");
}
public void run(){
System.out.println("run begin...");
System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
System.out.println("this.getName = "+ this.getName());
System.out.println("run end...");
}
}
运行测试:
package cn.sun.test;
import cn.sun.method.Demo02;
public class Test01 {
public static void main(String[] args) {
Demo02 demo = new Demo02();
Thread thread = new Thread(demo);
thread.setName("A");
thread.start();
}
}
结果如下:
demo01 begin...
Thread.currentThread().getName() = main
this.getName = Thread-0
demo02 end...
run begin...
Thread.currentThread().getName() = A
this.getName = Thread-0
run end...
2. isAlive()方法
判断当前线程是否处于活动状态
首先来个例子看一看怎么回事:
package cn.sun.method;
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("run = "+this.isAlive());
}
}
测试类:
package cn.sun.test;
import cn.sun.method.Demo02;
import cn.sun.method.MyThread;
public class Test01 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
System.out.println("begin =="+myThread.isAlive());
myThread.start();
System.out.println("end =="+myThread.isAlive());
}
}
然后我们发现结果是这样子的:
begin ==false
end ==true
run = true
总得来说这个方法不是太难,关键要分清楚某一时刻是不是出于活动状态,还有分清楚Thread.currentThread().getName()与this.getName()的区别。最后来个例子,看例子比较清楚
package cn.sun.method;
public class CountOperate extends Thread{
public CountOperate(){
System.out.println("CountOperate begin...");
System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
System.out.println("this.getName()="+this.getName());
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("CountOperate end...");
}
@Override
public void run() {
System.out.println("run begin...");
System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
System.out.println("this.getName()="+this.getName());
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("run end...");
}
}
测试类:
package cn.sun.test;
import cn.sun.method.CountOperate;
import cn.sun.method.Demo02;
import cn.sun.method.MyThread;
public class Test01 {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
Thread thread = new Thread(countOperate);
System.out.println("main begin threat isAlive = "+thread.isAlive());
thread.setName("A");
thread.start();
System.out.println("main end thread isAlive = "+thread.isAlive());
}
}
结果如下:
CountOperate begin...
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
CountOperate end...
main begin threat isAlive = false
main end thread isAlive = true
run begin...
Thread.currentThread().getName()=A
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
run end...
3.this和Thread.currentThread()区别:
Thread.currentThread() 返回当前线程对象,也就是执行这句代码这个线程。
this,也是当前线程对象~~~~
如果线程是start()方法调用起来的,那么二者是相等的。
不过,run()方法有可能被直接调用,这种情况下就不相等了。
new Thread("aaa"){
public void run(){
System.out.println(Thread.currentThread() == this);
new Thread("bbb"){
public void run(){
System.out.println(Thread.currentThread() == this);
}
}.start();
new Thread("ccc"){
public void run(){
System.out.println(Thread.currentThread() == this);
}
}.run();
}
}.start();
结果是:
true
true
false
第3个ccc线程是用run()方法调用的,那么就相当于new了一个普通类,调用了它的一个方法,仅此而已。
Thread.currentThread()返回的是aaa线程,this是ccc对象,所以不相等。
二.sleep()和getId()
1.sleep()方法
在指定的毫秒数内让当前正在执行的线程休眠,这里的当前线程就是this.currentThread()返回的线程。。
简单举个例子看一下:
package cn.sun.method;
public class SleepDemo extends Thread{
@Override
public void run() {
try {
System.out.println("run threadName "+this.currentThread().getName()+" begin "+System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("run threadName "+this.currentThread().getName()+" end "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类:
package cn.sun.test;
import cn.sun.method.CountOperate;
import cn.sun.method.Demo02;
import cn.sun.method.MyThread;
import cn.sun.method.SleepDemo;
public class Test01 {
public static void main(String[] args) {
SleepDemo s = new SleepDemo();
System.out.println("begin="+System.currentTimeMillis());
s.start();
System.out.println("end="+System.currentTimeMillis());
}
}
结果是:
begin=1460526284619
end=1460526284619
run threadName Thread-0 begin 1460526284620
run threadName Thread-0 end 1460526286620
2. getId()方法
便是线程唯一标示符
package cn.sun.test;
import cn.sun.method.CountOperate;
import cn.sun.method.Demo02;
import cn.sun.method.MyThread;
import cn.sun.method.SleepDemo;
public class Test01 {
public static void main(String[] args) {
Thread runThread = Thread.currentThread();
System.out.println("name="+runThread.getName()+" id="+runThread.getId());
}
}
得到的是:
name=main id=1
0 1
- java多线程入门学习(二)
- java多线程学习(二)
- java多线程学习(二)
- java多线程学习(二)
- java多线程学习(二)
- java多线程学习(二)
- Java多线程 快速入门(二)
- Java 入门 多线程 之 多线程返回用户ID(二)
- java入门学习(二)
- Java入门学习(二)
- java多线程学习二
- java多线程学习二
- 【JAVA学习】(二)JAVA 多线程同步
- 多线程入门(二)
- java入门学习(15)—多线程
- java多线程入门学习(一)
- java多线程入门学习(三)
- 学习JAVA多线程程序设计(二)
- 从网上整理了一些关于比较时间大小和请求网络时间的代码,在这里展示一下
- 一切又如从前
- VScleaner.bat
- Mysql命令
- 如斯·如诗
- java多线程入门学习(二)
- 也许是适当的人
- java 读取,写入 txt 文件示例
- 自定义View封装
- 图的创建、广度优先搜索、深度优先搜索
- 管理Spring容器中的自定义Bean
- redis在window下安装
- 2008年3月31日 12:00
- C#浮点数保留两位小数的方法