public class ThreadTest { public static void main(String[] args) { Thread evenThread = new Thread(new PrintEven(),"打印奇数"); Thread oddThread = new Thread(new PrintOdd(),"打印偶数"); evenThread.start(); oddThread.start(); } } class Count{ pu
这里只介绍最简单的方法 sed实现一:奇数行 sed -n 'p;n' file偶数行 sed -n 'n;p' file 这个可能比较难理解,举个网上的例子: 例,从aaa文件中取出偶数行cat aaa This is 1 This is 2 This is 3 This is 4 This is 5 sed -n 'n;p' aaa //-n表示取消默认输出 This is 2 This is 4 注 释:sed读取This is 1,执行n命令
打印出0-10之间的所有偶数: 方法1: 1 num = 2 2 while num < 10: 3 print(num) 4 num += 2 方法2: 1 num = 1 2 while num < 10: 3 if num%2 == 0: 4 print(num) 5 num +=1 打印出0-10之间的所有奇数: 方法1: 1 num = 1 2 while num < 10: 3 print(num) 4 num += 2 方法2: 1 num = 0 2 while num
1. 直接用CAS中的AtomicInteger package concurency.chapter13; import java.util.concurrent.atomic.AtomicInteger; /** * @auther draymonder */ public class PrintOddAndEven { public static volatile boolean flag = false; public static AtomicInteger num = new Ato
两个线程如何交替打印出奇数和偶数 分析 两个线程交替打印奇数和偶数,最关键的是如何协作的问题. 打印的数可以用java里面的atomicInteger 来保证原子性: 打印何时结束需要设置一个上限,比如打印到100结束: 1 public class PrintABAtomic{ 2 private static final int MAX_PRINT_NUM = 100; 3 private static final AtomicInteger atomicInteger = new Atom