第一组
sleep和getname( )是静态方法
- 线程的优先级
yield成功与否,取决于cpu的资源是否紧张。若cpu资源丰富,则不成功。
join可以理解为A线程在运行之后(即start之后)插入到B线程中,并且阻塞B线程继续进行。
join方法的练习public class main3 {
public static void main(String[] args) throws InterruptedException {
q q1 = new q();
Thread thread1 = new Thread(q1);
for(int i = 1 ;i<=10;i++){
System.out.println("hi"+i);
Thread.sleep(1000);
if(i == 5){
thread1.start();
thread1.join();
}
}
}
}
class q implements Runnable {
@Override
public void run() {
for(int i = 1;i<=10;i++){
System.out.println("hello"+i);
try{
Thread.sleep(1000);
}
catch(Exception e ){
e.printStackTrace();
}
}
}
}
第三组
setDaemon(true)的使用。
package com.hspedu.method;
public class ThreadMethod03 {
public static void main(String[] args) throws InterruptedException {
MyDaemonThread myDaemonThread = new MyDaemonThread();
//如果我们希望当main线程结束后,子线程自动结束
//,只需将子线程设为守护线程即可
myDaemonThread.setDaemon(true);
myDaemonThread.start();
for( int i = 1; i <= 10; i++) {//main线程
System.out.println("宝强在辛苦的工作...");
Thread.sleep(1000);
}
}
}
class MyDaemonThread extends Thread {
public void run() {
for (; ; ) {//无限循环
try {
Thread.sleep(1000);//休眠1000毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("马蓉和宋喆快乐聊天,哈哈哈~~~");
}
}
}



