zoukankan      html  css  js  c++  java
  • 线程按照顺序打印

    package com.cn.test.thread;
    
    /**
     *abc三个线程顺序打印十次 
     */
    public class TestSortedThread extends Thread{
        
        static int  threadFlag = 1;
        
        public TestSortedThread(String string) {
            super(string);
        }
        
        @Override
        public void run() {
            for (int i=0; i<10; i++) {
                 sortThread();
            }
        }
    
        // 线程排序
        private void sortThread() {
            while (threadFlag == 1) {
                if (Thread.currentThread().getName() == "thread-a") {
                    System.out.println("A");
                    threadFlag ++;
                } else {
                    Thread.currentThread().yield();
                }
            } 
            while (threadFlag == 2) {
                if (Thread.currentThread().getName() == "thread-b") {
                    System.out.println("B");
                    threadFlag ++;
                } else {
                        Thread.currentThread().yield();
                   }
            } 
            while (threadFlag == 3) {
                if (Thread.currentThread().getName() == "thread-c") {
                    System.out.println("C");
                    threadFlag = threadFlag -2 ;
                } else {
                    Thread.currentThread().yield();
                   }
            }
            
        }
    
        public static void main(String[] args) {
            TestSortedThread threadA = new TestSortedThread("thread-a");
            TestSortedThread threadB = new TestSortedThread("thread-b");
            TestSortedThread threadC = new TestSortedThread("thread-c");
            threadA.start();
            threadB.start();
            threadC.start();
        }
    
    
  • 相关阅读:
    排序之快速排序
    希尔排序
    大数据的乘法
    大数据的乘法实现——C语言
    js函数纪实
    【转】js中$含义及用法
    python基础操作
    git 常用指令
    Django框架学习记录
    【转】Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次
  • 原文地址:https://www.cnblogs.com/startSeven/p/10229158.html
Copyright © 2011-2022 走看看