您的位置:首页 > 编程语言 > Java开发

java多线程的学习心得

2016-07-04 09:27 357 查看
多线程没怎么用过,前几天经过同事提点。开始从多线程的功能上下手,于是呢,先写了一个线程循环交替打印的例子。

package com.thread;
/**
* 线程交替执行(通信:锁)
* 效果是1,2,A,3,4,B,5,6,C.......交替打印
* 线程a打印1,2
* 线程b打印A,B
*/
public class SignalCommunication {
public static final Object obj = new Object();
public static void main(String[] args) {
Thread a = new Thread(new Thread0());
Thread b = new Thread(new Thread1());
try {
a.start();
Thread.sleep(100);
b.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Thread0 implements Runnable {
private int num = 1;
@Override
public void run() {
while (num < 52) {
synchronized (SignalCommunication.obj) {
try {
SignalCommunication.obj.notify();
System.out.println(num);
num ++;
System.out.println(num);
num ++;
SignalCommunication.obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Thread1 implements Runnable {
private int ch = 65;
@Override
public void run() {
while (ch < 91) {
synchronized (SignalCommunication.obj) {
try {
SignalCommunication.obj.notify();
System.out.println((char)ch);
ch ++;
SignalCommunication.obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}原理很简单,就是两个线程公用同一把锁SignalCommunication.obj,线程A得到锁之后,打印1,2,然后锁的状态是等待,线程A让出系统资源,线程B开始执行。同理,线程B执行完后,阻塞锁,让出资源。由此,同事得到一个问题,如果是多个线程交替执行呢。还能不能用这种方式。然后鼓捣了一天,写了出来
package com.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* 多个线程轮流打印,这里用到线程池
* @author GUCHUANG192
*
*/
public class MutiThread {
public static boolean flag = false;

public static void main(String[] args) {
int num = 5; //线程数量
doExecute(num);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static void doExecute(int num) {
ExecutorService pool = Executors.newFixedThreadPool(num);
Object[] oArray = new Object[num];
for (int i=0;i<num;i++) {
Object o = new Object();
oArray[i] = o;
}
for (int i = 0; i < oArray.length; i ++) {
try {
Thread6 t;
if (i == 0) {
t = new Thread6(oArray[oArray.length-1], oArray[i], (char)(i+65), num);
} else {
t = new Thread6(oArray[i-1], oArray[i], (char)(i+65), num);
}
Thread.sleep(100); //确保线程顺序执行
pool.execute(t);
if (i == oArray.length) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
pool.shutdownNow();
} catch (Exception e) {
e.printStackTrace();
}

}
}

class Thread6 extends Thread{
private char name; //打印字符
private Object prev; //当前线程执行前一个线程的锁
private Object curr; //当前线程的锁
private int count; //循环次数

public Thread6 (Object prev,Object curr,char name, int count) {
this.prev = prev;
this.curr = curr;
this.name = name;
this.count = count;
}

public void run () {
for (int i = 0; i < count; i ++) {
synchronized (prev) {
synchronized (curr) {
curr.notify();
System.out.println(Thread.currentThread().getName() + ":" + name);
}
try {
prev.wait();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
}

方法是参照网上看到的一个三个线程交替执行。由于忘了地址,没法引用。是酱紫的,每个线程拥有两把锁,一把锁是上一个线程的,一个是自己的。当前线程必须拥有上一个执行线程的锁,才可以往下执行。这样,第一个线程拥有最后一个线程的锁和自身的锁,完成闭合。循环执行,当然在执行前需要使用Thread.sleep(100);控制下执行顺序。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程