- 分门类的管理:线程组
public class ThreadGroupTest implements Runnable{
@Override
public void run() {
String groupName = Thread.currentThread().getThreadGroup().getName()
+ "-" + Thread.currentThread().getName();
while (true) {
System.out.println("this is " + groupName );
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("testGroup");
Thread t1 = new Thread(threadGroup,new ThreadGroupTest(),"T1");
Thread t2 = new Thread(threadGroup,new ThreadGroupTest(),"T2");
t1.start();
t2.start();
System.out.println("线程组执行数:" + threadGroup.activeCount());
threadGroup.list();
}
}
执行结果:
线程组执行数:2
java.lang.ThreadGroup[name=testGroup,maxpri=10]
Thread[T1,5,testGroup]
Thread[T2,5,testGroup]
this is testGroup-T1
this is testGroup-T2
this is testGroup-T2
this is testGroup-T1
this is testGroup-T2
this is testGroup-T1
...
线程组有一个值得注意的方法 stop(), 会停止线程组所有线程。
但是它会遇到和 Thread.stop() 相同的问题。
- 驻守后台:守护线程(Daemon) - 垃圾回收线程、JIT线程
当一个 Java应用内,只有守护线程时,Java虚拟机就会自然退出。
public class DaemoDemo {
public static class DaemoT extends Thread {
@Override
public void run() {
while (true) {
System.out.println("this is daemon thread");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
DaemoT t = new DaemoT();
t.setDaemon(true);
t.start();
Thread.sleep(100);
}
}
只需将 setDaemon() 设为true,必须在 start() 调用之前
- 线程优先级
在Java中,使用 1 到 10 表示线程优先级。内置三个静态标量
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
数字越大优先级越高。
public class PriorityDemo {
public static class HightPriority extends Thread {
static int count = 0;
@Override
public void run() {
while (true) {
synchronized (PriorityDemo.class) {
count++;
if(count > 100000) {
System.out.println("HightPriority is complete");
break;
}
}
}
}
}
public static class LowPriority extends Thread {
static int count = 0;
@Override
public void run() {
while (true) {
synchronized (PriorityDemo.class) {
count++;
if (count > 100000) {
System.out.println("LowPriority complete");
break;
}
}
}
}
}
public static void main(String[] args) {
Thread hight = new HightPriority();
Thread low = new LowPriority();
hight.setPriority(Thread.MAX_PRIORITY);
low.setPriority(Thread.MIN_PRIORITY);
low.start();
hight.start();
}
}
执行结果:
HightPriority is complete
LowPriority complete
- 线程安全的概念与 synchronized
public class AccountVol implements Runnable {
static AccountVol instance = new AccountVol();
static volatile int i = 0;
public static synchronized void increase() {
i++;
}
@Override
public void run() {
for (int j = 0;j < 100000;j++) {
/*synchronized (instance) {
increase();
}*/
increase();
}
}
public static void main(String[] args) throws InterruptedException {
/* Thread t1 = new Thread(instance);
Thread t2 = new Thread(instance);*/
Thread t1 = new Thread(new AccountVol());
Thread t2 = new Thread(new AccountVol());
t1.start();t2.start();
t1.join();t2.join();
System.out.println(i);
}
}
- 并发下的 ArrayList
public class ThreadForArrayList {
static List<Integer> al = new ArrayList<Integer>();
public static class AddThread implements Runnable {
@Override
public void run() {
for (int i = 0;i < 100000;i++) {
al.add(i);
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new AddThread());
Thread t2 = new Thread(new AddThread());
t1.start();t2.start();
t1.join();t2.join();
System.out.println(al.size());
}
}
执行结果:
1.
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 10
at java.util.ArrayList.add(ArrayList.java:459)
at com.xc.blackCap.thread.ThreadForArrayList$AddTrade.run(ThreadForArrayList.java:17)
at java.lang.Thread.run(Thread.java:745)
100005
2. 数组 size 小于 200000
改进方法:Vector 代替 ArrayList
- 并发下的 HashMap
public class ThreadForHashMap {
//static Map<String,String> map = new HashMap<String,String>();
static Map<String,String> map = new ConcurrentHashMap<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 < 100000;i+=2) {
map.put(String.valueOf(i),Integer.toBinaryString(i));
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new AddThread(0));
Thread t2 = new Thread(new AddThread(1));
t1.start();t2.start();
t1.join();t2.join();
System.out.println(map.size());
}
}
调整:使用 ConcurrentHashMap 代替 HashMap