线程常用方法
【小城贝尔】
线程常是用户类,不跑完毕不干醉。
守护线程好进退,无需在意何时累。
礼让睡眠全静态,直接调用无阻碍。
睡眠方法抛异常,抱住资源难松放。
礼让直归就绪态,共争资源公平在。
加入方法插队信,你不完毕难推进。
设置优先有顺序,大小仅是看概率。
设置获得新名称,是否存活待求真。
返回此时的线程,时长书写心中神。
线程分为守护线程和用户线程,
默认都是用户线程
jvm停止不用等待守护线程结束
class DaemonThread{
public static void main(String[] args) {
God god = new God();
Thread tgod = new Thread(god);
//设置为守护线程
tgod.setDaemon(true);
tgod.start();
Person person = new Person();
Thread tPerson = new Thread(person);
tPerson.start();
}
//static方法中只可以调用static 变量 成员内部类特性类似于成员变量
static class God implements Runnable{
@Override
public void run() {
for (;true;){
System.out.println("god is blessing you ............");
}
}
}
static class Person implements Runnable{
@Override
public void run() {
for (int i = 0; i < 365*100; i ++){
System.out.println("today is fine ......"+i);
if(i >= 365*100){
System.out.println("i dead.....");
}
}
}
}
}
Thread.sleep();
class Sleep{
public static void main(String[] args) {
//使用lambda写法 写一个倒计时 sleep方法会抱住资源休息 而wait不会
new Thread(()->{
Date da = new Date(System.currentTimeMillis() + 1000*10);
long end = da.getTime();
while (true){
System.out.println(new SimpleDateFormat("mm:ss").format(da));
da = new Date(da.getTime() - 1000);
if(end - 10000 > da.getTime()){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
Thread.yield();
//yield 礼让线程 会直接退回到就绪状态而不是阻塞状态
class Yield{
public static void main(String[] args) {
for (; true; ) {
new Thread(()->{
System.out.println("礼让开始 ,,,,,");
Thread.yield();
System.out.println("礼让结束 ,,,,,");
},"Have got yield").start();
new Thread(()->{
System.out.println("没有设置礼让");
},"none").start();
}
}
/*
礼让结束 ,,,,,
礼让开始 ,,,,,
没有设置礼让
礼让结束 ,,,,,
没有设置礼让
*/
}
join();
//join 插队线程 那个线程使用这个方法则这个线程开始运行 等待这个线程运行完毕之后被他阻塞的线程才可运行
class Join{
//讲述老爸让儿子买烟的story
public static void main(String[] args) {
new Thread(new Father()).start();
}
static class Father implements Runnable{
@Override
public void run() {
System.out.println("Father want to have a cigarette 。。。。。。。");
System.out.println("Give son 10 pounds and sak him to buy a box of cigarette");
Thread sonThread = new Thread(new Son());
sonThread.start();
try {
//加载线程启动之后 因为写在Father线程中所以阻塞Father线程等到Son线程执行完毕Father线程才继续执行
sonThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("open the box of cigarette and give the change to son....");
System.out.println("starting smoking");
}
}
static class Son implements Runnable{
@Override
public void run() {
System.out.println("catch the father's money");
System.out.println("find a game hall");
System.out.println("play for three hours");
for (int i = 1; i <= 4;i ++ ){
System.out.println("i have played : "+i +" hours");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("game over ......");
System.out.println("bought one box of cigarette....");
System.out.println("give cigarette and change to father...");
}
}
}
setPriority();
class Priority{
public static void main(String[] args) {
Thread t1 = new Thread(()->{
System.out.println("==============> "+Thread.currentThread().getName());
},"nike");
Thread t2 = new Thread(()->{
System.out.println("==============> "+Thread.currentThread().getName());
},"PIKE");
Thread t3 = new Thread(()->{
System.out.println("==============> "+Thread.currentThread().getName());
},"PUMA");
Thread t4 = new Thread(()->{
System.out.println("==============> "+Thread.currentThread().getName());
},"回力");
//设置优先级
t3.setPriority(Thread.MAX_PRIORITY);
t4.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
t4.start();
/*
设置优先级 提高的试运行概率
==============> PUMA
==============> 回力
==============> nike
==============> PIKE
*/
}
}
其他方法
class ElseMethod{
static class Name{
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println("name : "+Thread.currentThread().getName());// name : new Name
},"初始化mame");
//设置线程名称
t.setName("new Name");
t.start();
}
}
static class IsAlive{
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println("IsAlive ......");
});
t.start();
while (t.isAlive()){
System.out.println("isAlive ======>" +t.isAlive());
}
System.out.println("isAlive ======>" +t.isAlive());
}
}
}