zoukankan      html  css  js  c++  java
  • java线程

    今天重新把昨晚的线程同步面试题做一遍时,发现实际情况运行下来时,线程一直不同步。后来经过不断测试,发现自己的一个误区。

    之前一直以为,线程如果被唤醒后再次执行时,会从头开始运行这个线程,也就是重新运行Runnable中的run()方法;

    而实际情况是,被唤醒并且被执行的线程是从上次阻塞的位置从下开始运行,也就是从wait()方法后开始执行。

    所以判断是否进入某一线程的条件 是用while判断,而不是用If判断判断。

    下面举例说明:

    如果三个线程同步工作,第一个线程打印1,2,3,4,5   ,然后第二个线程打印1,2,3,4,5  ,接着第三个线程也打印 1,2,3,4,5,重复100次。

     1 public class Thread_test {
     2     
     3     static final manage m = new manage();
     4     
     5     public static void main(String[] args) {
     6         //线程一
     7         new Thread(new Runnable() {            
     8             @Override
     9             public void run() {
    10                 // TODO Auto-generated method stub
    11                 for(int i=0;i<50;i++){
    12                     m.threadPrint(i,0);
    13                 }
    14             }
    15         }).start();
    16         
    17         //线程二
    18         new Thread(new Runnable(){
    19             
    20             public void run(){
    21                 for(int i=0;i<50;i++){
    22                     m.threadPrint(i,1);
    23                 }
    24             }
    25         }).start();
    26         
    27                 //线程三
    28         new Thread(new Runnable(){
    29             
    30             public void run(){
    31                 for(int i=0;i<50;i++){
    32                     m.threadPrint(i,2);
    33                 }
    34             }
    35         }).start();
    36 
    37     }
    38 
    39 }
    40 
    41 
    42 //控制线程执行的顺序
    43 class manage{
    44        //权限,0代表1号线程执行,1代表二号线程执行,2代表三号线程执行
    45     private int isA = 0;
    46     
    47     public synchronized void threadPrint(int i,int n){
    48           /*该处应用while来判断是否轮到该线程执行,假如用If判断的话,如果该线程阻塞后再次被唤醒执行时(其他线程调用this.notifyAll()),
    他会从this.wait()后面的代码开始执行,即时没轮到该线程执行*/
    49 while(this.isA != n){ 50 try {
    this.wait(); 53 } catch (Exception e) { 54 // TODO: handle exception 55 } 56 } 57 for(int j=0;j<5;j++){ 58 System.out.println(Thread.currentThread().getName()+":"+(j+1)+" loop in "+(i+1)); 59 } 60 this.isA = (this.isA+1)%3;//将权限传递给后一个进程62 this.notifyAll(); 63 } 64 65 66 67 }
  • 相关阅读:
    Codeforces Round #443 Div. 1
    linux中ps命令
    占cpu 100%的脚本
    检查Linux系统cpu--内存---磁盘的脚本
    jQuery对象的属性操作
    关于js的一些收集
    Linux命令集合
    使用python操作excel表格
    Linux7.3系统 升级python到3.6使用ping主机脚本
    一个别人的线程池的编写
  • 原文地址:https://www.cnblogs.com/sallybin/p/3991525.html
Copyright © 2011-2022 走看看