网站制作广告/建立网站费用大概需要多少钱
Thread的常用方法
1.start()方法启用线程并执行相应run()方法
2.run():子线程要执行代码放入run()方法中
3.currentThread():静态的,调取当前线程
4. getName():获取线程名字
5.setName():设置线程名字
6.yield():调用此方法的线程释放当前CPU执行权
7.join():在A线程中调用B线程的join方法表示:A停止B执行完毕再A执行
8.isAlive():判断当前线程是否执行完毕
9.sleep(long 1):显示的让当前线程睡眠1 ms
10。线程通信:wait(); notify() notifyAll()
设置线程的优先级
getPriority():返回线程优先级
setPriority(int new Priority):改变线程的优先级强到资源概率变大,先执行是join();
class SubThread1 extends Thread{public void run() {for(int i=0;i<100;i++) {
// try {
// Thread.currentThread().sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }System.out.println(Thread.currentThread().getName()+Thread.currentThread().getPriority()+":"+i);}}
}
public class testThread12 {public static void main(String[] args) {SubThread1 st1 = new SubThread1();st1.setName("子线程");st1.setPriority(Thread.MAX_PRIORITY);st1.start();Thread.currentThread().setName("======主线程");for(int i = 1;i<100;i++) {
// if(i%10==0) {
// Thread.currentThread().yield();//释放当前CPU执行权
// }
// if(i==20) {
// try {
// st1.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }System.out.println(Thread.currentThread().getName()+":"+i);}}
}