当前位置: 移动技术网 > IT编程>开发语言>Java > Java 多线程实例详解(二)

Java 多线程实例详解(二)

2019年07月22日  | 移动技术网IT编程  | 我要评论
本文承接上一篇文章《java多线程实例详解(一)》。 四.java多线程的阻塞状态与线程控制 上文已经提到java阻塞的几种具体类型。下面分别看下引起java线程阻塞的

本文承接上一篇文章《java多线程实例详解(一)》。

四.java多线程的阻塞状态与线程控制

上文已经提到java阻塞的几种具体类型。下面分别看下引起java线程阻塞的主要方法。

1.join()

join —— 让一个线程等待另一个线程完成才继续执行。如a线程线程执行体中调用b线程的join()方法,则a线程被阻塞,知道b线程执行完为止,a才能得以继续执行。

public class threadtest {

 public static void main(string[] args) {

 myrunnable myrunnable = new myrunnable();
 thread thread = new thread(myrunnable);

 for (int i = 0; i < 100; i++) {
  system.out.println(thread.currentthread().getname() + " " + i);
  if (i == 30) {
  thread.start();
  try {
   thread.join(); // main线程需要等待thread线程执行完后才能继续执行
  } catch (interruptedexception e) {
   e.printstacktrace();
  }
  }
 }
 }
}

class myrunnable implements runnable {

 @override
 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println(thread.currentthread().getname() + " " + i);
 }
 }
}

2.sleep()

sleep —— 让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。在其睡眠的时间段内,该线程由于不是处于就绪状态,因此不会得到执行的机会。即使此时系统中没有任何其他可执行的线程,出于sleep()中的线程也不会执行。因此sleep()方法常用来暂停线程执行。

前面有讲到,当调用了新建的线程的start()方法后,线程进入到就绪状态,可能会在接下来的某个时间获取cpu时间片得以执行,如果希望这个新线程必然性的立即执行,直接调用原来线程的sleep(1)即可。

public class threadtest {

 public static void main(string[] args) {

 myrunnable myrunnable = new myrunnable();
 thread thread = new thread(myrunnable);

 for (int i = 0; i < 100; i++) {
  system.out.println(thread.currentthread().getname() + " " + i);
  if (i == 30) {
  thread.start();
  try {
   thread.sleep(1); // 使得thread必然能够马上得以执行
  } catch (interruptedexception e) {
   e.printstacktrace();
  }
  }
 }
 }
}

class myrunnable implements runnable {

 @override
 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println(thread.currentthread().getname() + " " + i);
 }
 }
}

注:睡一个毫秒级够了,因为cpu不会空闲,会切换到新建的线程。

3.后台线程(daemon thread)

概念/目的:后台线程主要是为其他线程(相对可以称之为前台线程)提供服务,或“守护线程”。如jvm中的垃圾回收线程。

生命周期:后台线程的生命周期与前台线程生命周期有一定关联。主要体现在:当所有的前台线程都进入死亡状态时,后台线程会自动死亡(其实这个也很好理解,因为后台线程存在的目的在于为前台线程服务的,既然所有的前台线程都死亡了,那它自己还留着有什么用...伟大啊 ! !)。

设置后台线程:调用thread对象的setdaemon(true)方法可以将指定的线程设置为后台线程。

public class threadtest {

 public static void main(string[] args) {
 thread mythread = new mythread();
 for (int i = 0; i < 100; i++) {
  system.out.println("main thread i = " + i);
  if (i == 20) {
  mythread.setdaemon(true);
  mythread.start();
  }
 }
 }

}

class mythread extends thread {

 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println("i = " + i);
  try {
  thread.sleep(1);
  } catch (interruptedexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
  }
 }
 }
}

判断线程是否是后台线程:调用thread对象的isdeamon()方法。

注:main线程默认是前台线程,前台线程创建中创建的子线程默认是前台线程,后台线程中创建的线程默认是后台线程。调用setdeamon(true)方法将前台线程设置为后台线程时,需要在start()方法调用之前。前天线程都死亡后,jvm通知后台线程死亡,但从接收指令到作出响应,需要一定的时间。

4.改变线程的优先级/setpriority():

每个线程在执行时都具有一定的优先级,优先级高的线程具有较多的执行机会。每个线程默认的优先级都与创建它的线程的优先级相同。main线程默认具有普通优先级。

设置线程优先级:setpriority(int prioritylevel)。参数prioritylevel范围在1-10之间,常用的有如下三个静态常量值:

max_priority:10

min_priority:1

norm_priority:5

获取线程优先级:getpriority()。

注:具有较高线程优先级的线程对象仅表示此线程具有较多的执行机会,而非优先执行。

public class threadtest {

 public static void main(string[] args) {
 thread mythread = new mythread();
 for (int i = 0; i < 100; i++) {
  system.out.println("main thread i = " + i);
  if (i == 20) {
  mythread.setpriority(thread.max_priority);
  mythread.start();
  }
 }
 }

}

class mythread extends thread {

 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println("i = " + i);
 }
 }
}

5.线程让步:yield()

上一篇博文中已经讲到了yield()的基本作用,同时,yield()方法还与线程优先级有关,当某个线程调用yiled()方法从运行状态转换到就绪状态后,cpu从就绪状态线程队列中只会选择与该线程优先级相同或优先级更高的线程去执行。

public class threadtest {

 public static void main(string[] args) {
 thread mythread1 = new mythread1();
 thread mythread2 = new mythread2();
 mythread1.setpriority(thread.max_priority);
 mythread2.setpriority(thread.min_priority);
 for (int i = 0; i < 100; i++) {
  system.out.println("main thread i = " + i);
  if (i == 20) {
  mythread1.start();
  mythread2.start();
  thread.yield();
  }
 }
 }

}

class mythread1 extends thread {

 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println("mythread 1 -- i = " + i);
 }
 }
}

class mythread2 extends thread {

 public void run() {
 for (int i = 0; i < 100; i++) {
  system.out.println("mythread 2 -- i = " + i);
 }
 }
}

 系列文章:

j
java 多线程实例详解(二)
java 多线程实例详解(三)

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网