zoukankan      html  css  js  c++  java
  • Java 线程控制(输出奇偶数)

    两个线程,一个输出1,3,5,7......99;另一个输出2,4,6,8......100。

    1.线程同步

     1 public class ST2 {
     2     int i = 0;
     3 
     4     public static void main(String[] args) {
     5         ST2 st1 = new ST2();
     6         new Thread(st1.new Inc()).start();
     7         new Thread(st1.new Dec()).start();
     8     }
     9 
    10     private synchronized void inc(){
    11         i++;
    12         try {
    13             Thread.currentThread().sleep(1000);
    14         } catch (InterruptedException e) {
    15             e.printStackTrace();
    16         }
    17         System.out.println(Thread.currentThread().getName()+" +++  "+i);
    18     }
    19     
    20     private synchronized void dec(){
    21         i--;
    22         try {
    23             Thread.currentThread().sleep(1000);
    24         } catch (InterruptedException e) {
    25             e.printStackTrace();
    26         }
    27         System.out.println(Thread.currentThread().getName()+" -----  "+i);
    28     }
    29     
    30     class Inc implements Runnable{
    31         @Override
    32         public void run() {
    33             while(true)
    34                 inc();
    35         }
    36     }
    37     
    38     class Dec implements Runnable{
    39         @Override
    40         public void run() {
    41             while(true)
    42                 dec();
    43         }
    44     }
    45 }

    输出:

    Thread-1 -----  -1
    Thread-1 -----  -2
    Thread-0 +++  -1
    Thread-0 +++  0
    Thread-0 +++  1
    Thread-0 +++  2
    Thread-1 -----  1
    Thread-0 +++  2
    Thread-1 -----  1
    Thread-1 -----  0
    Thread-1 -----  -1
    Thread-1 -----  -2
    Thread-0 +++  -1
    Thread-0 +++  0
    Thread-0 +++  1
    Thread-1 -----  0
    Thread-1 -----  -1
    Thread-1 -----  -2
    Thread-1 -----  -3
    Thread-1 -----  -4
    Thread-0 +++  -3
    Thread-0 +++  -2
    Thread-0 +++  -1
    Thread-0 +++  0

    从这里可以看出,线程随机执行。

    2.控制线程执行顺序

     1     private synchronized void inc() throws Exception{
     2         b = true;
     3         notifyAll();
     4         i++;
     5         System.out.println(Thread.currentThread().getName()+" ++  "+i);
     6         
     7         Thread.currentThread().sleep(1000);
     8 
     9         while(b)
    10             wait();
    11     }
    12     
    13     private synchronized void dec() throws Exception {
    14         while(!b)
    15             wait();
    16         i++;
    17         System.out.println(Thread.currentThread().getName()+" ++++  "+i);
    18         
    19         Thread.currentThread().sleep(1000);
    20          
    21         b = false;
    22         notifyAll();
    23     }

    输出:

    Thread-0 ++  1
    Thread-1 ++++  2
    Thread-0 ++  3
    Thread-1 ++++  4
    Thread-0 ++  5
    Thread-1 ++++  6
    Thread-0 ++  7
    Thread-1 ++++  8
    Thread-0 ++  9
    Thread-1 ++++  10
    Thread-0 ++  11
    Thread-1 ++++  12
    Thread-0 ++  13
    Thread-1 ++++  14
    Thread-0 ++  15
    Thread-1 ++++  16
    Thread-0 ++  17
    Thread-1 ++++  18
    Thread-0 ++  19
    Thread-1 ++++  20
    Thread-0 ++  21

    3.控制线程执行次数

     1 class Inc implements Runnable{
     2             @Override
     3             public void run() {
     4                     for(int j=0 ; j<50; j++){
     5                             try {
     6                                 inc();
     7                             } catch (Exception e) {
     8                                 e.printStackTrace();
     9                             }
    10                     }
    11             }
    12     }
    13     
    14     class Dec implements Runnable{
    15             @Override
    16             public void run() {
    17                 for(int j=0 ; j<50; j++){
    18                             try {
    19                                 dec();
    20                             } catch (Exception e) {
    21                                 e.printStackTrace();
    22                             }
    23                     }
    24             }
    25     }
  • 相关阅读:
    链表问题(5)-----读取
    链表问题(4)----环形链
    服务器上安装anaconda
    链表问题(3)-----反转
    (1)安装vagrant和virtualbox
    链表问题(2)-----删除
    链表问题(1)----遍历和实现
    Python笔记26----正则表达式匹配
    读书笔记--SQL必知必会04--过滤数据
    Docker
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3858889.html
Copyright © 2011-2022 走看看