遇到一个问题
2个class
ThreadA会打印出6行:每行间隔100ms
1,Thread A
2,Thread A
3,Thread A
4,Thread A
5,Thread A
6,Thread A
ThreadB也会打印出六行:每行间隔100ms
1,Thread B
2,Thread B
3,Thread B
4,Thread B
5,Thread B
6,Thread B如何使得两个线程有序的打印出来? 先打印完A 在打印B
我想用到synchronised Syste。out 但是具体怎么使用不知道 求解。

解决方案 »

  1.   

    这个问题 不涉及synchronised
      

  2.   

    使用wait notify方法 
      

  3.   


    请问如何使用wait notify的方法啊? 如果大家好心可以给点代码么? 看例子可能我会比较容易理解一点
      

  4.   

    wait notify为最佳实现,如果jdk1.5 你可以用 信号量
    package com.ssj.test;public class NotifySynch {
    private Object lock = new Object();
    class ThreadA extends Thread{
    @Override
    public void run() {
    for(int i = 0 ; i < 6 ; i++){
    System.out.println("ThreadA");
    }
    synchronized(lock){
    lock.notifyAll();
    }
    }
    }

    class ThreadB extends Thread{
    @Override
    public void run() {
    synchronized(lock){
    try {
    lock.wait();
    } catch (InterruptedException e) {

    }
    }
    for(int i = 0 ; i < 6 ; i++){
    System.out.println("ThreadB");
    }
    }
    }

    public void testRun(){
    new ThreadB().start();
    new ThreadA().start();
    }
    public static void main(String[] args) {
    new NotifySynch().testRun();
    }
    }
      

  5.   

    package TD;
    public class ThreadA implements Runnable { private int i;

    @Override
    public void run() {
    // TODO Auto-generated method stub
    synchronized(System.out){
    i=2;
    System.out.printf("thread A:\n"); while(i<7){
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } System.out.printf(i+", thread A\n");
    i++;

    }

    }
    notify();}
    }
    package TD;public class ThreadB extends Thread { private int i;

    @Override
    public void run() {
    // TODO Auto-generated method stub
    synchronized(System.out){
    try {
    wait();
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    i=2;
    System.out.printf("thread B:\n"); while(i<7){
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    System.out.printf(i+", thread B\n");
    i++;
    }
    }
    }}package TD;public class Main { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ThreadA ta=new ThreadA();
    ThreadB tb=new ThreadB();
    Thread t=new Thread(ta);
    t.start();
    tb.start();


    }
    }
      

  6.   

    这个是居于信号量的package com.ssj.test;import java.util.concurrent.Semaphore;public class SemaphoreTest {
    Semaphore sem ;
    public SemaphoreTest(){
    sem = new Semaphore(0);
    }

    class ThreadA extends Thread{
    @Override
    public void run() {
    for(int i = 0 ; i < 6 ; i++){
    System.out.println("ThreadA");
    }
    sem.release();
    }
    }

    class ThreadB extends Thread{
    @Override
    public void run() {
    try {
    sem.acquire();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    for(int i = 0 ; i < 6 ; i++){
    System.out.println("ThreadB");
    }
    }
    }
    public void testRun(){
    new ThreadB().start();
    new ThreadA().start();

    }
    public static void main(String[] args) {
    System.out.println("use SemaphoreTest");
    new SemaphoreTest().testRun();
    }
    }
      

  7.   

    package TD;
    public class ThreadA implements Runnable { private int i;

    @Override
    public void run() {
    // TODO Auto-generated method stub

    i=2;
    System.out.printf("thread A:\n"); while(i<7){
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } System.out.printf(i+", thread A\n");
    i++;

    }

    synchronized(System.out){
    System.out.notify();
    }
    }
    }
    改threada  感谢帮助