public class TestYild {
public static void main(String[] args) {
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start(); t2.start();
}
}
class MyThread3 extends Thread {
MyThread3(String s){super(s);}
public void run(){
for(int i =1;i<=100;i++){
System.out.println(getName()+": "+i);
if(i%10==0){
yield();<span style="white-space:pre"> </span>//作用是让出CPU一次
}
}
}
}
public class TestJoin {
public static void main(String[] args) {
MyThread4 ss = new MyThread4("ss");
ss.start();
try{
ss.join();<span style="white-space:pre"> </span>//join是将当前线程合并,在这里是将ss线程合并到main函数中
} catch(InterruptedException e) {}
for(int i=0;i<10;i++){
System.out.println("main:" + i);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
}
}
}
class MyThread4 extends Thread{
MyThread4(String s) {
super(s);
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(getName() + i);
try{
sleep(1000);
} catch(InterruptedException e) {
return;
}
}
}
}
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY +3);<span style="white-space:pre"> </span>//这里是指设置优先级,<span style="font-family: Arial, Helvetica, sans-serif;">NORM_PRIORITY这是默认的优先级,是5</span>
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("T1:" + i);
}
}
}
class T2 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println(" T2:" + i);
}
}
}