Thread类中的常用方法
1:start()
启动当前线程,调用当前线程的run()方法
2:run()
通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中
案例1:
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
for(int i = 0; i <= 100; i++){
System.out.print("Good!");
}
}
}
package threadStudy;
public class MyThread extends Thread{
public void run(){
for(int i = 0; i <= 100; i++){
System.out.print("Nice!");
}
}
}
3:currentThred()
静态方法,返回执行当前代码的线程
4:getName()
获取当前线程的名称
5:setName()
设置当前线程的名称
案例2:
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
Thread.currentThread().setName("MainThread");
for(int i = 0; i <= 100; i++){
System.out.print("Good!");
System.out.println(Thread.currentThread().getName());
}
}
}
package threadStudy;
public class MyThread extends Thread{
public void run(){
for(int i = 0; i <= 100; i++){
System.out.print("Nice!");
}
}
}
6:yield()
释放当前cpu的执行权
案例3
通过构造函数,设置线程的名称,以及通过yield()方法释放当前cpu的执行权。
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread("MyThread");
mythread.start();
Thread.currentThread().setName("MainThread");
for(int i = 0; i <= 100; i++){
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
}
package threadStudy;
public class MyThread extends Thread{
private String threadName;
public MyThread(String threadName){
super(threadName);
}
public void run(){
for(int i = 0; i <= 100; i++){
if(i % 2 == 0){
this.yield();
}
System.out.println(this.currentThread().getName()+"***"+i);
}
}
}
7:join()
在线程a中调用b的join(),此时线程a就进入阻塞状态,直到线程b完成执行完成以后,线程a才结束阻塞的状态
面试题: 有三个线程T1,T2,T3,怎么确保它们按顺序执行?
在多线程中有多种方法让线程按特定顺序执行,你可以用线程类的join()方法在一个线程中启动另一个线程,另外一个线程完成该线程继续执行。为了确保三个线程的顺序你应该先启动最后一个(T3调用T2,T2调用T1),这样T1就会先完成而T3最后完成。
案例4
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread("MyThread");
mythread.start();
Thread.currentThread().setName("MainThread");
for(int i = 0; i <= 100; i++){
System.out.println(Thread.currentThread().getName()+"-"+i);
if(i == 10){
try {
mythread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package threadStudy;
public class MyThread extends Thread{
private String threadName;
public MyThread(String threadName){
super(threadName);
}
public void run(){
for(int i = 0; i <= 100; i++){
System.out.println(this.currentThread().getName()+"***"+i);
}
}
}
8:sleep()
使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。
金典段子:
项目经理要求在某个代码中运行变慢,好让客户给钱优化,修改后并得到明显的速度提升
Thread.sleep(20000);//停止20秒
面试题:Java多线程中调用wait() 和 sleep()方法有什么不同?
Java程序中wait 和 sleep都会造成某种形式的暂停,它们可以满足不同的需要。wait()方法用于线程间通信,如果等待条件为真且其它线程被唤醒时它会释放锁,而 sleep()方法仅仅释放CPU资源或者让当前线程停止执行一段时间,但不会释放锁。
案例5
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread("MyThread");
mythread.start();
Thread.currentThread().setName("MainThread");
for(int i = 0; i <= 100; i++){
System.out.println(Thread.currentThread().getName()+"-"+i);
if(i == 10){
try {
mythread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package threadStudy;
public class MyThread extends Thread{
private String threadName;
public MyThread(String threadName){
super(threadName);
}
public void run(){
for(int i = 0; i <= 100; i++){
if(i == 10){
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.currentThread().getName()+"***"+i);
}
}
}
9:isAlive()
==测试这个线程是否活着。 ==
案例6:
package threadStudy;
public class Main {
public static void main(String[] args) {
MyThread mythread = new MyThread("MyThread");
mythread.start();
Thread.currentThread().setName("MainThread");
for(int i = 0; i <= 100; i++){
System.out.println(Thread.currentThread().getName()+"-"+i);
if(i == 10){
try {
mythread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("mythread is alive "+mythread.isAlive());
}
}
}
package threadStudy;
public class MyThread extends Thread{
private String threadName;
public MyThread(String threadName){
super(threadName);
}
public void run(){
for(int i = 0; i <= 100; i++){
if(i == 10){
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.currentThread().getName()+"***"+i);
}
}
}