一、三个线程顺序打印ABC10次
解决方案一:适用三个标志来标识哪个线程应该等待,分别在三个线程中使用同步代码块。
public class solution{
private static boolean flag1=true;
private static boolean flag2=false;
private static boolean flag3=false;
public static void main(String[] args){
final solution o=new solution();
Thread t1=new Thread(new Runnable(){
public void run(){
for(int i=0;i<10;i++){
while(true){
synchronized(o){
if(!flag1){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(Thread.currentThread().getName());
flag1=false;
flag2=true;
flag3=false;
o.notifyAll();
break;
}
}
}
}
}
},"A");
Thread t2=new Thread(new Runnable(){
public void run(){
for(int i=0;i<10;i++){
while(true){
synchronized(o){
if(!flag2){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(Thread.currentThread().getName());
flag1=false;
flag2=false;
flag3=true;
o.notifyAll();
break;
}
}
}
}
}
},"B");
Thread t3=new Thread(new Runnable(){
public void run(){
for(int i=0;i<10;i++){
while(true){
synchronized(o){
if(!flag3){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(Thread.currentThread().getName());
flag1=true;
flag2=false;
flag3=false;
o.notifyAll();
break;
}
}
}
}
}
},"C");
t1.start();
t2.start();
t3.start();
}
}
解决方案二:使用原子类 AtomicInteger类 ,AtomicInteger类线程安全,适合在高并发中当计数器.
class threadTest extends Thread{
private AtomicInteger synobj;
private String name;//打印的字母
private int flag;//线程标识
private int count=0;//统计打印的次数。
public threadTest(AtomicInteger synobj,String name,int flag){
this.synobj=synobj;
this.name=name;
this.flag=flag;
}
public void run(){
while(true){
synchronized(synobj){
if(synobj.get()%3==flag){
synobj.set(synobj.get()+1);
System.out.println(name);
count++;
synobj.notifyAll();
if(count==10){
break;
}else{
try {
synobj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
public class solution{
public static void main(String[] args){
AtomicInteger synobj=new AtomicInteger(0);
threadTest t1=new threadTest(synobj,"A",0);
threadTest t2=new threadTest(synobj,"B",1);
threadTest t3=new threadTest(synobj,"C",2);
t1.start();
t2.start();
t3.start();
}
}
二、要求用三个线程,按顺序打印1,2,3,4,5.... 71,72,73,74, 75.
线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到线程3打印到75。
解决思路:创建一个线程对应的类型,三个线程分开处理。分别调用三个不同的线程,循环打印5个数字,打印完以后唤醒其他休眠的线程,然后自己休眠。
public class sortedAlgorithm {
static int type = 1;//线程对应的类型,三个线程分开处理
static int number = 1;//打印数字的起始
public static void main(String[] args) {
new Thread("第一个线程:") { // 创建一个线程,命名为第一个线程
public void run() {
try {
synchronized (sortedAlgorithm.class) {
while (number <= 75) {
if (type == 1) {//当type的值为1的时候,由第一个线程来打印
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + number++);
}
type = 2;//修改类型值
sortedAlgorithm.class.notifyAll();//唤醒其他进入休眠的线程
} else {
sortedAlgorithm.class.wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread("第二个线程:") { // 创建二个线程,命名为第二个线程
public void run() {
try {
synchronized (sortedAlgorithm.class) {
while (number <= 75) {
if (type == 2) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + number++);
}
type = 3;
sortedAlgorithm.class.notifyAll();
} else {
sortedAlgorithm.class.wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread("第三个线程:") { // 创建三个线程,命名为第三个线程
public void run() {
try {
synchronized (sortedAlgorithm.class) {
while (number <= 75) {
if (type == 3) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + number++);
}
type = 1;
sortedAlgorithm.class.notifyAll();
if (number < 76)
sortedAlgorithm.class.wait();
} else {
sortedAlgorithm.class.wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
}