要求:使用定时器,间隔 4 秒执行一次,再间隔 2 秒执行一次,以此类推执行
package multiThread;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest extends TimerTask {
private static volatile int count = 0;
@Override
public void run() {
count = (count+1)%2; //结果为0或1交替
System.err.println("执行定时任务,隔两秒四秒交替打印");
new Timer().schedule(new TimerTest(),2000+2000*count);
}
public static void main(String[] args) {
Timer timer = new Timer(); //设置一个定时器
timer.schedule(new TimerTest(),2000); //2秒后执行TimerTest中的run方法
while(true){ //每隔一秒打印一次
System.out.println(Calendar.getInstance().get(Calendar.SECOND));
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
用面向对象的方法求出数组中重复 value 的个数,按如下个数输出:
1 出现:1 次
3 出现:2 次
8 出现:3 次
2 出现:4 次
int[] arr = {1,4,1,4,2,5,4,5,8,7,8,77,88,5,4,9,6,2,4,1,5};
package com.xzq;
import java.util.HashMap;
import java.util.Map;
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {1, 4, 1, 4, 2, 5, 4, 5, 8, 7, 8, 77, 88, 5, 4, 9, 6, 2, 4, 1, 5};
ArrayDemo arrayDemo = new ArrayDemo();
arrayDemo.countAndSout(arr);
}
public void countAndSout(int arr[]) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
int a = arr[i];
if (!map.containsKey(a)) {
map.put(a, 1);
} else {
Integer integer = map.get(a);
integer++;
map.put(a, integer);
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
Integer key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " 出现:" + value + "次");
}
}
}
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
int[] arr = { 1, 8, 2, 2, 8, 9, 8, 0, 6, 8, 7, 4, 5, 8, 3, 2, 2, 3 };
calculate(arr);
}
public static void calculate(int[] arr) {
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i]) + 1);
} else {
map.put(arr[i], 1);
}
}
System.out.println("统计后,输出前:");
System.out.println(map);
int n = map.size();
for (int i = 0; i < n; i++) {
System.out.println(map.firstKey() + "出现:" + map.get(map.firstKey()) + "次");
map.remove(map.firstKey());
}
System.out.println("输出后(由于map不能根据index下标索引来获取元素,只能一个一个获取第
一个键值对,然后删除来实现,所以最终为空的):");
System.out.println(map);
}
}
要求:子线程运行执行 10 次后,主线程再运行 5 次。这样交替执行三遍
public static void main(String[] args) {
final Bussiness bussiness = new Bussiness();
//子线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
bussiness.subMethod();
}
}
}).start();
//主线程
for (int i = 0; i < 3; i++) {
bussiness.mainMethod();
}
}
}
class Bussiness {
private boolean subFlag = true;
public synchronized void mainMethod() {
while (subFlag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+ " : main thread running loop count -- " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
subFlag = true;
notify();
}
public synchronized void subMethod() {
while (!subFlag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread().getName() + " : sub thread running loop count -- " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
subFlag = false;
notify();
}
}
/**
* 子线程运行执行10次后,主线程再运行5次。这样交替执行三遍
*/
public class _02_Interview {
public static void main(String[] args) {
Business bussiness = new Business();
//子线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
bussiness.subMethod();
}
}
}).start();
//主线程
for (int i = 0; i < 3; i++) {
bussiness.mainMethod();
}
}
}
//这个类是执行任务的类
class Business {
private boolean flag = false;
//flag为true,主线程执行
public synchronized void mainMethod() {
//是不是子线程在执行?是,继续等子线程执行完
while (false == flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//执行主线程任务
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + i);
}
//轮到子线程执行了
flag = false;
//唤醒子线程。(一共就两个线程,子线程和主线程,所以唤醒的只能是子线程。)
notify();
}
//flag为false,子线程执行
public synchronized void subMethod() {
//是不是主线程在执行?是,继续等主线程执行完
while (true == flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//执行子线程任务
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + i);
}
//轮到主线程执行了
flag = true;
//唤醒主线程。(一共就两个线程,子线程和主线程,所以唤醒的只能是主线程。)
notify();
}
}