一,两个线程交替打印
public class TwoThreadPrint {
public static class Printer {
private Boolean flag = false;
public synchronized void printA() {
try {
while (!flag) {
wait();
}
System.out.println(Thread.currentThread().getName() + " ---- A");
flag = !flag;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void printB() {
try {
while (flag) {
wait();
}
System.out.println(Thread.currentThread().getName() + " ---- B");
flag = !flag;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static class ThreadA extends Thread {
private Printer printer;
public ThreadA(Printer printer) {
this.printer = printer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
this.printer.printA();
}
}
}
public static class ThreadB extends Thread {
private Printer printer;
public ThreadB(Printer printer) {
this.printer = printer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
this.printer.printB();
}
}
}
public static void main(String[] args) throws Exception {
Printer printer = new Printer();
Thread threadA = new ThreadA(printer);
Thread threadB = new ThreadB(printer);
threadA.start();
threadB.start();
}
}
二, 给定一个能够生成均匀1~5随机枚举数的函数,请设计一个能够生成均匀1~7随机枚举数的函数。参考这里
public class Rand57 {
private static Random random = new Random();
public static int rand5() {
return random.nextInt(5);
}
private static int[][] initArray = new int[5][5];
public static int rand7() {
int col = Rand57.rand5();
int row = Rand57.rand5();
if (col == 4 && row > 0) {
return Rand57.rand7();
} else {
return Rand57.initArray[col][row] % 7;
}
}
static {
int val = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
Rand57.initArray[i][j] = (val % 7);
val++;
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(Rand57.rand7());
Map<String, Integer> resultMap = new HashMap<String, Integer>();
for (int i = 0; i < 10000000; i++) {
String val = String.valueOf(Rand57.rand7());
if (null == resultMap.get(val)) {
resultMap.put(val, 1);
} else {
resultMap.put(val, resultMap.get(val).intValue() + 1);
}
}
for (String key : resultMap.keySet()) {
System.out.println(key + "----" + resultMap.get(key));
}
}
}
三,对指定的N个数字排序
public class SortRandomNumber {
public static void main(String[] args) throws Exception {
File file = new File("random_number.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
long start = System.currentTimeMillis();
byte[] location = new byte[10000000];
for (int i = 0; i < location.length; i++) {
location[i] = 0;
}
File outFile = new File("sort_number.txt");
outFile.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
String line = br.readLine();
while (StringUtils.isNotBlank(line)) {
location[Integer.valueOf(line)] = 1;
line = br.readLine();
}
boolean first = true;
for (int i = 0; i < location.length; i++) {
if (location[i] == 1) {
if (!first) {
bw.newLine();
}
bw.write(String.valueOf(i));
first = false;
}
}
long end = System.currentTimeMillis();
System.out.println("total time: " + (end -start));
br.close();
bw.flush();
bw.close();
}
}