《 .NET全栈开发VIP项目实战》最新

### 第一题:

/**  
 *   1、经典多线程题目。
 *   
 *  启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 
 *  然后是线程3打印11,12,13,14,15. 
 *  接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75
 * */
public class Thread02 {
 
    
      public static void main(String[] args) {
          
          Object obj = new Object();
          new Thread(new PrintRunnable(obj,1)).start();
          new Thread(new PrintRunnable(obj,2)).start();
          new Thread(new PrintRunnable(obj,3)).start();
          
          
    }
}

class  PrintRunnable implements Runnable{
    //volatile在这里可加可不加。我试了几次,结果都是对的。
    
      private volatile static int printNum = 0;
      
      private Object o;
      
      private int threadId ;
      
      
      public PrintRunnable(Object o, int threadId) {
        
        this.o = o;
        this.threadId = threadId;
    }


    public void run(){
             
        synchronized(o){
            
            while(printNum<75){

            //这个算法不错。
                 if(printNum/5%3+1==threadId){
                     for(int i=0;i<5;i++){
                        // printNum++;
                         System.out.println("线程"+threadId+":"+(++printNum));
                    
                     }
                     o.notifyAll();
                     
                 }else{
                     try {
                         //wait方法的含义:就是调用此方法后,此线程处于阻塞,
                         //直到它被唤醒,唤醒后,就继续执行wait方法后的语句。而不会
                         //再执行if里的语句了。要理解哈。
                         
                        o.wait();
                        
                    //    System.out.println(threadId+":等待");
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                     
                 }
                
            }
            
            
        }
        
          
      }
}

##  第二题:

/** 
 * 
 *  2、现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
 *  */
public class Thread03 {
 
    
     public static void main(String[] args) {
        Thread03.method02();
        
         
    }
     
     private static void method02(){
            Runnable runnable = new Runnable() {
                @Override public void run() {
                    System.out.println(Thread.currentThread().getName() + "执行完成");
                }
            };
            //在非main方法中也可以new对象啊。
            Thread t1 = new Thread(runnable, "t1");
            Thread t2 = new Thread(runnable, "t2");
            Thread t3 = new Thread(runnable, "t3");
            try {
                t1.start();
                //此处的join方法表示一定会在t2线程先执行完。
                
                t1.join();
                t2.start();
                
              
                t2.join();
                t3.start();
                t3.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
     }
}


## 第三题:

 public static void main(String[] args) {
          
          //3、编写10个线程,第一个线程从1加到10,第二个线程从11加到20.....第十个线程从91加到100,
          //最后再把10个线程结果相加。
          Object obj = new Object();
          
          for(int i=0;i<10;i++){
              new ThreadZhou(obj,(i+1)).start();
          }
        
    }
}

//
class ThreadZhou extends Thread{
      private static int sum=0;
      
      private int threadId;
      
      private Object obj;
      private static int num=0;
      //一定要加上static
     static private int[] arr = new int[10];
      
      public ThreadZhou(Object obj,int threadId){
          this.obj = obj;
                  
          this.threadId = threadId;
      }
     
       public void run(){
           
             synchronized(obj){
                 
                 while(num<100){
                      if(num/10%10+1==threadId){
                           for(int i=0;i<10;i++){
                               num++;
                               arr[threadId-1]+=num;
                              
                           }
                           System.out.println((threadId-1)+":"+arr[threadId-1]);
                            if(num==100){
                                
                                 for(int j=0;j<arr.length;j++){
                                     //气死我了。??结果不对??
                                      System.out.println("j:"+arr[j]);
                                      sum+=arr[j];
                                  }
                                  System.out.println("sum:"+sum);
                            }
                          obj.notifyAll();
                      }else{
                          try {
                              
                            obj.wait();
                        } catch (InterruptedException e) {
                            
                            e.printStackTrace();
                        }
                          
                      } 
                     
                 }
                 /*if(num==100){
                     
                 }*/
                /* if(num==100){
                        
                     System.out.print("\n");
                     for(int j=0;j<arr.length;j++){
                         //气死我了。??结果不对??
                          System.out.println("j:"+arr[j]);
                          sum+=arr[j];
                      }
                      System.out.println("sum:"+sum);
                }*/
                 
             }
       }
}
 

##第四题:

生产者、消费者情形,之前也看到过,没有研究,今日花了点时间理解了哈。大有裨益。棒棒的。

有个共享资源类,Person类。

public class Person {
 
    
     private String name;
     
     private int age;
     //表示共享资源对象是否为空,如果为true,表示需要生产,如果为false,表示有数据了,不要生产。
     private boolean isEmpty=true;
     
     public synchronized void push(String name,int age){
         try {
         if(!isEmpty){
            
                this.wait();
            } 
         
         //
         this.name = name;
         this.age = age;
         
         isEmpty = false;
         
         System.out.println("person:"+this);
         this.notifyAll();
           }catch (InterruptedException e) {
                
                e.printStackTrace();
            }         
            
     }
     
     //取数据
     public synchronized void pop(){
         try{
             
             if(isEmpty){
                 this.wait();
                 
             }
             
             System.out.println(this.name+"---"+this.age);
             
             isEmpty = true;
             this.notifyAll();
             
         }catch(InterruptedException e){
             e.printStackTrace();
             
         }
     }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
     
     
}
  

再写个类Thread05。调用main方法。就是多线程中的生产者-消费者模型了。

public class Thread05 {
 
    
     public static void main(String[] args) {
         
         Person p = new Person();
         
         Runnable producer = new Producer(p);
         Runnable consumer = new Consumer(p);
         
         Thread pro = new Thread(producer);
         Thread con = new Thread(consumer);
         
         pro.start();
         con.start();
         
         
    }
}

class Producer implements Runnable{
    
     Person p = null;
     
     public Producer(Person p){
         this.p = p;
         
     }
        
     public void run(){
         
          for(int i=0;i<50;i++){
              if(i%2==0){
                  p.push("Tom", 11);
                  
              }else{
                  p.push("Marry", 20);
              }
          }
     }
}

class Consumer implements Runnable{
    
      Person p = null;
      
      public Consumer(Person p){
          this.p = p;
      }
      
      public void  run(){
          
          for(int i=0;i<50;i++){
              p.pop();
              
          }
      }
}

## 第五题:

这个例子是演示wait方法与notify方法,即多线程通信的简单的例子。

public class ThreadWait01 {
 
    
      public static void main(String[] args) {
          
          Object obj = new Object();
          Thread t1 = new Wait(obj);
          
          Thread t2 = new   Notify(obj);
          
          t1.start();
          t2.start();
          
          
    }
}

class Wait extends Thread{
     private Object obj;
     
     public Wait(Object obj){
           this.obj = obj;
     }
    
      public void run(){
          
          
         
               //模拟尽量让Wait类先执行。
          //Thread.sleep(1000);
            Thread.yield();
        
           
          synchronized(obj){
              for(int i=0;i<30;i++){
                  Example.i++;
                  
                  if(i==11){
                      obj.notify();
                      System.out.println("已经发出唤醒消息了");
                  }
                  
                  System.out.println("i:"+i);
              }
              
          }
         
          
      }
     
}

class  Notify extends Thread{
      private Object obj;
      
      public Notify(Object obj){
          this.obj = obj;
      }
      
       public void run(){
           
            synchronized(obj){
                if(Example.i!=11){
                    try {
                        System.out.println("wait start");
                        obj.wait();
                        
                        System.out.println("wait end");
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                    
                }
            }
           
       }
    
}

class Example{
    
      static int i=0;
      
}

完结。


--------------------- 
作者:HermioneZhou 
来源:CSDN 
原文:https://blog.csdn.net/qq_36142320/article/details/89061842 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/wangjun0916/article/details/90030212