1.线程中的常用操作方法
Java.lang.Thread类【线程类】
void | start() 启动线程 |
static Thread | currentThread()得到当前正在运行的线程对象 |
String | getName()返回该线程的名称。 |
void | setName(String name)设置线程名称
|
package com.wangxing.threadDemo1;
public class TestThread implements Runnable{
@Override
public void run(){
//static Thread currentThread();得到当前正在运行的线程对象
Thread th=Thread.currentThread();
//String getName();返回该线程的名称
String thname=th.getName();
for(int i=1;i<=100;i++){
System.out.println(thname+"--i="+i);
}
}
}
package com.wangxing.threadDemo1;
public class TestMain {
public static void main(String args[]){
TestThread th1=new TestThread();
Thread thread1=new Thread(th1);
Thread thread2=new Thread(th1);
/*
* void setName(String name);设置线程名称
* 1.当没有设置线程名称时,系统会默认赋予线程一个名称"Thread-0","Thread-1"...
*/
thread1.setName("线程1");
thread2.setName("线程2");
//void start(); 启动线程
thread1.start();
thread2.start();
String name=Thread.currentThread().getName();
System.out.println("主线程名称="+name);
}
}
int | getPriority() 返回线程的优先级。 |
void | setPriority(int newPriority) 更改线程的优先级。 |
线程的优先级--就是线程的执行先后的概率。
- 线程的优先级有10个级别,分别使用整数1~10来表示。
为了方便操作,java将10个级别有规定成3个级别,分别是最低的优先级,中等优先级,最高的优先级,并且将这3个级别封装成了静态常量
static int | MAX_PRIORITY 线程可以具有的最高优先级。10 |
static int | MIN_PRIORITY线程可以具有的最低优先级。1 |
static int | NORM_PRIORITY分配给线程的默认优先级。5 |
2.设置线程的优先级的时候,数字越大优先级越高,数字越小优先级越低。优先级越高并代表就一定会优先执行,只是被优先执行的几率增大,因此不要试图通过控制线程的优先级,来保证某一个线程,总是第一个执行。
3.所有线程默认的优先级都是5【中等级别】
package com.wangxing.threadDemo2;
public class TestThread implements Runnable{
@Override
public void run(){
//static Thread currentThread()得到当前正在运行的线程名称
Thread th=Thread.currentThread();
//String getName();返回该线程的名称
String name=th.getName();
for(int i=1;i<=100;i++){
System.out.println(name+"--i+="+i);
}
}
}
package com.wangxing.threadDemo2;
public class TestMain {
public static void main(String args[]){
TestThread testthread=new TestThread();
Thread th1=new Thread(testthread);
Thread th2=new Thread(testthread);
th1.setName("线程1");
th2.setName("线程2");
/*
* void setPriority(int newPriority)更改线程的优先级
* 1.线程的优先级有10个级别,分别使用整数1~10来表示
* 为了方便操作,java将十个级别规定成3个级别,分别是最低的优先级,中等优先级,低级优先级
* 并且将这三个级别封装撑了静态常量
*/
// th1.setPriority(1);
// th2.setPriority(10);
// Thread.currentThread().setPriority(5);
th1.setPriority(Thread.MAX_PRIORITY);
th2.setPriority(Thread.MIN_PRIORITY);
Thread th3=Thread.currentThread();
th3.setPriority(Thread.NORM_PRIORITY);
//int getPriority();返回线程的优先级
//所有线程默认的优先级都是5,【中等优先级】
System.out.println(th1.getName()+"优先级是"+th1.getPriority());
System.out.println(th2.getName()+"优先级是"+th2.getPriority());
// System.out.println(th3.getName()+"优先级是"+th3.getPriority());
//得到主线程的名称
String name=th3.getName();
//得到主线程的优先级
int ys=th3.getPriority();
System.out.println(name+"优先级是"+ys);
//2.设置线程优先级的时候,数字越大优先级越高,数字越小优先级越低
//优先级越高,并不代表就一定会先执行,只是被优先执行的 几率 大一些
//因此不要试图通过控制线程优先级,来保证某一线程,总是第一个执行
th1.start();
th2.start();
}
}
守护线程的相关操作方法
通常情况之下我们所创建的线程都是普通线程,非守护线程,也叫用户线程。
守护线程,也叫精灵线程【当所有用户线程都执行完毕以后,自动结束运行的线程就是守护线程】
特征:当所有用户线程都执行完毕以后,无论守护线程能否可以继续运行,都要立刻停止运行。
例如:不是同年同月同日生,但是一定会同年同月同日死【共死/陪葬】
boolean | isDaemon() 测试该线程是否为守护线程。 |
void | setDaemon(boolean on) 将该线程标记为守护线程用户线程。 |
package com.wangxing.threadDemo3;
import java.util.concurrent.Callable;
public class ShouHuThread implements Callable{
@Override
public Object call() throws Exception {
//static Thread currentThread()得到当前正在运行的线程对象
Thread th=Thread.currentThread();
//String getName();得到该线程的名称
String name=th.getName();
while(true){
Thread.sleep(1000);//设置时间间隔0.5秒
System.out.println(name);
}
}
}
package com.wangxing.threadDemo3;
public class TestThread implements Runnable{
@Override
public void run() {
//static Thread currentThread();得到当前正在运行的线程对象
Thread th=Thread.currentThread();
//String getName();返回该线程的名称
String name=th.getName();
for(int i=1;i<=20;i++){
try {
Thread.sleep(500);//设置间隔0.6秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"--i=="+i);
}
}
}
package com.wangxing.threadDemo3;
import java.util.concurrent.FutureTask;
public class TestMain {
public static void main(String args[]){
//创建用户线程目标对象
TestThread testThread=new TestThread();
Thread th1=new Thread(testThread);
//创建守护线程目标对象
ShouHuThread sh=new ShouHuThread();
FutureTask ft=new FutureTask(sh);
Thread th2=new Thread(ft);
th1.setName("用户线程");
th2.setName("守护线程");
//void setDaemon(boolean on);将此线程设置为守护线程
th2.setDaemon(true);
//boolean isDaemon();测试该线程是否为守护线程
System.out.println(th1.getName()+"-isDameon="+th1.isDaemon());
System.out.println(th2.getName()+"-isDemaon="+th2.isDaemon());
//特征:当所有用户线程都执行完毕后,无论守护线程能否可以继续执行,都要立刻停止
th1.start();
th2.start();
}
}
static void | sleep(long millis) 设置线程休眠【暂停】指定的时间【毫秒】 |
void | interrupt() 中断线程休眠【暂停】。 |
package com.wangxing.threadDemo4;
import java.util.concurrent.Callable;
public class TestThread implements Runnable{
@Override
public void run() {
//static Thread currentThread()得到当前正在运行的线程对象
Thread th=Thread.currentThread();
//String getName();得到该线程的名称
String name=th.getName();
System.out.println(name+"开始听课");
try {
th.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"进入梦乡");
try{
th.sleep(10000);
}catch(Exception e){
System.out.println("被老师踹醒,又开始听课");
}
try{
th.sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("下课啦!");
}
}
package com.wangxing.threadDemo4;
import java.util.concurrent.FutureTask;
public class TestMain {
public static void main(String args[])throws Exception{
//创建目标对象
Thread th=new Thread(new TestThread());
System.out.println("上课铃响了!");
th.setName("小明");
th.start();
Thread.sleep(7000);
System.out.println("老师发现同学上课睡着了");
System.out.println("老师走过去,踹了一脚!");
//interrupt() 中断线程休眠【暂停】。会进入异常InterruptedException
th.interrupt();
}
}
package com.wangxing.threadDemo4;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class NaoZhong {
public static void main(String args[]) throws InterruptedException{
System.out.println("请设置闹钟时间");
Scanner input=new Scanner(System.in);
String settime=input.nextLine();
boolean flag=true;
SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd HH-mm-ss");
while(flag){
Thread.sleep(1000);
Date date=new Date();
String time=sdf.format(date);
System.out.println(time);
if(settime.equals(time)){
System.out.println("闹钟时间到!!!!");
flag=false;
}
}
}
}
void | join(long millis)等待该线程终止的时间最长为 millis 毫秒。【强制线程执行】 |
package com.wangxing.threadDemo5;
import java.util.concurrent.Callable;
public class TestThread implements Callable{
@Override
public Object call() throws Exception {
//static Thread currentThread()得到当前正在运行的线程对象
Thread th=Thread.currentThread();
//String getName();得到该线程的名称
String name=th.getName();
for(int i=0;i<=50;i++){
Thread.sleep(500);
System.out.println(name+"--i"+i);
}
return null;
}
}
package com.wangxing.threadDemo5;
import java.util.concurrent.FutureTask;
public class TestMain {
public static void main(String args[]) throws InterruptedException{
//Thread th=new Thread(new TestThread());
//TestThread testThread=new TestThread();
FutureTask ft=new FutureTask(new TestThread());
Thread th1=new Thread(ft);
th1.setName("线程1");
th1.start();
Thread th2=Thread.currentThread();
th2.setName("主线程main");
//得到主线程的名称
String name=Thread.currentThread().getName();
for(int j=0;j<=50;j++){
th1.sleep(500);
System.out.println(name+"--j"+j);
if(j==25){
Thread.currentThread().join(3000);
}
}
}
}
2.线程的生命周期
1.线程的生命周期就是线程从一开始创建,到run方法执行完毕以后的状态变化。[状态之间的切换]
2.线程的生命周期几种状态【1、新建状态 2、就绪状态 3、运行状态 4.阻塞状态 5.死亡状态】
创建状态:通过new的方式创建出线程对象,此时线程就进入到创建状态【新建状态】。
新建状态的线程是不能运行。
就绪状态:新建状态的线程调用strat方法之后就会进入就绪状态。
就绪状态的线程具备执行能力,但是没有cpu资源。【万事具备只差cpu】.
运行状态:就绪状态的线程得到cpu资源开始执行run方法,此时这个线程就是运行状态。
运行状态的线程当cpu切换到其他线程时候,本线程就再一次进入就绪状态。
阻塞状态:运行状态的线程调用sleep/wait方法......此时线程进入阻塞状态。
处于阻塞状态的线程的休眠时间到/调用notify方法/notifyAll方法在此时线程进 入就绪状态,从就绪状态中得到cpu资源从而进入运行状态。
死亡状态:运行状态的线程run方法运行结束/线程执行过程中遇到异常/调用stop方法此时 线程就进入到死亡状态。
死亡状态的线程是不能运行,除非再一次使用strat方法重新启动运行。