java 多线程 HashMap CPU占满

import java.util.HashMap;
import java.util.Map;


public class HashMapMultiThread {
static Map<String,String> map = new HashMap<String,String>();
public static class AddThread implements Runnable{
int start = 0 ;
public AddThread(int start){
this.start = start;
}
@Override
public void run() {
for (int i = 0; i < 10000; i+=2) {
map.put(Integer.toString(i), Integer.toBinaryString(i));
}
}

public static void main(String[] args) throws InterruptedException {

// 根据你的电脑CPU核数来配置 两核启两个线程就行

Thread t1 = new Thread(new HashMapMultiThread.AddThread(0));
Thread t2 = new Thread(new HashMapMultiThread.AddThread(1));
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(map.size());
}
}
}

你可能感兴趣的:(java,多线程,cpu,HashMap)