当前位置:编程学习 > JAVA >>

线程输出

用两个线程输出如下字符串
12A34B56C。。。。。。5152Z --------------------编程问答--------------------

public class Test implements Runnable{
//Object o = new Object();
boolean flag = true;
int num = 1;
char let = 'A';
public static void main(String[] args) throws InterruptedException {
Test t = new Test(true);
new Thread(t).start();
Thread.sleep(10);
t.setFlag(false);
new Thread(t).start();
}

public Test(boolean flag){
this.flag =  flag;
}

synchronized void printNumber(){
while(let <= 'Z'){
System.out.print(num++);
if(num % 2 == 1)
try {
this.notify();
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized void printLetter(){
while(let <= 'Z'){
System.out.print(let);
let++;
this.notify();
if(let <= 'Z')
{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void run() {
if(flag)
printNumber();
else
printLetter();
}

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}
}

--------------------编程问答-------------------- 功能实现了,不具备什么通用性

如果有多个线程按顺序执行某些动作,这种方式无能为力 --------------------编程问答--------------------
引用 2 楼 cjf068 的回复:
功能实现了,不具备什么通用性

如果有多个线程按顺序执行某些动作,这种方式无能为力

线程锁 --------------------编程问答-------------------- for example
public class Test {
    static int count = 0;

    public static void main(String[] args) {
        Object locker = new Object();
        new NumThread(locker).start();
        new AlphaThread(locker).start();
    }
}

class NumThread extends Thread {
    Object locker;
    int start = 1;
    public NumThread(Object locker) {this.locker = locker;}
    public void run() {
        while(true) {
            synchronized(locker) {
                if ((Test.count+1)%3 == 0) {
                    try {locker.wait();} catch (Exception e) {e.printStackTrace();}
                } else {
                    Test.count++;
                    System.out.printf((start++) + "");
                    if (start > 52) {start = 1;}
                    locker.notifyAll();
                }
            }
        }
    }


class AlphaThread extends Thread {
    Object locker;
    char start = 'A';
    public AlphaThread(Object locker) {this.locker = locker;}
    public void run() {
        while(true) {
            synchronized(locker) {
                if ((Test.count+1)%3 != 0) {
                    try {locker.wait();} catch (Exception e) {e.printStackTrace();}
                } else {
                    Test.count++;
                    System.out.printf((char)(start++) + "");
                    if (start > 'Z') {start = 'A';}
                    locker.notifyAll();
                }
            }
        }
    }

--------------------编程问答--------------------
引用 4 楼 qybao 的回复:
for example

Java code

public class Test {
    static int count = 0;

    public static void main(String[] args) {
        Object locker = new Object();
        new NumThread(locker).start()……


同步问题 。  这个写的很好 --------------------编程问答-------------------- 线程还是用Runnable好点,帮顶下 --------------------编程问答-------------------- 写了一个可以新的程序,可以指定:
顺序执行--------------------先new的先执行
每个任务每轮指定执行次数------new的时候第一个参数
一共运行多少轮---------------new 的时候第二个参数


public class Test implements Runnable{
private static int order = 0;
private static int total = 0;
private static Object lock = new Object(); //锁
private final int id;
private int times; //运行频率
private int totalTimes; //运行总次数
private GoToRun go;

/*
 * 构造函数
 * 构造的时候会隐含指定了顺序,先构造的先执行
 * 1.运行频率
 * 2.运行总次数
 */
public Test(int times, int totalTimes, GoToRun go){
id = total++;
this.times = times;
this.totalTimes = totalTimes;
this.go = go;
}
public void run() {
synchronized (lock){
for(int i = 0; i < totalTimes; i++){
while(order != id){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j = 0; j < times; j++)
go.goToRun();
order = (order + 1) % total;
lock.notifyAll();
}
}
}

public static void main(String[] args) {
synchronized (Test.lock){
new Thread(new Test(2, 26, new A())).start(); //第一个运行线程A的goToRun()方法,每轮运行2次,总共运行26次
new Thread(new Test(1, 26, new B())).start(); //第二个运行线程B的goToRun()方法,每轮运行1次,总共运行26次
//new Thread(new Test(1, 26, new C())).start(); //第三个运行线程C的goToRun()方法,每轮运行1次,总共运行26次
}
}
}

interface GoToRun{
void goToRun();
}
//要运行的方法所在的类继承GoToRun接口
class A implements GoToRun{
int i = 1;
public void goToRun() {
System.out.print(i++);
}
}
class B implements GoToRun{
char ch = 'A';
public void goToRun(){
System.out.print(ch++);
}
}
class C implements GoToRun{
char ch = 'a';
public void goToRun(){
System.out.print(ch++);
}
}
--------------------编程问答-------------------- 这么多回的我就不写了 
推荐一个强的 http://hi.baidu.com/dapplehou/blog/item/14e62834ebe1bc235ab5f504.html
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,