public class MutiThread extends Thread { private byte[] lock = new byte[0]; private int i; private MutiThread(int i) {
this.i = i;
} public void run() {
synchronized (lock) {
if (i == 1)
System.out.println("A");
if (i == 2)
System.out.println("B");
if (i == 3)
System.out.println("C");
}
} public static void main(String[] args) {
MutiThread mutiThread1 = new MutiThread(1);
mutiThread1.start();
MutiThread mutiThread2 = new MutiThread(2);
mutiThread2.start();
MutiThread mutiThread3 = new MutiThread(3);
mutiThread3.start();
}}我想要程序按照 A B C的顺序输出,加上了synchronized关键字也没效果。请解答~javasynchronized

解决方案 »

  1.   

    我觉得这题不要synchronized,直接用等待线程运行好再下一步的那个函数。
      

  2.   

    额 。我想说的是你加上了synchronized当然不一定会看到ABC顺序输出,我们不知道你的3个线程哪个先执行
    还有你的方法其实达不到同步效果,除非你把private byte[] lock = new byte[0]; 改成
    private static byte[] lock = new byte[0]; 
      

  3.   

    JVM的线程调度不是以显式的顺调度的,是随机调度的.synchronized 只是保证只有一个在执行而已
      

  4.   

    题目:编写一个多线程程序,有三个线程,第一个打印A,第二个打印B
    第三个打印C,使用线程同步,分别依次循环在屏幕上打印19个A,B,C
    如ABCABCABCABCABCABCABCABC....
      

  5.   

    我写的一个 lz可以参照下package com.djk.design.bulid;public class ThreadTest
    {
        public static void main(String[] args)
        {
            MyThread myThread1 = new MyThread();
            myThread1.start();
            MyThread myThread2 = new MyThread();
            myThread2.start();
            MyThread myThread3 = new MyThread();
            myThread3.start();
        }
    }class MyThread extends Thread
    {
        private static int i = 1;
        
        private static Object object =new Object();
        @Override
        public void run()
        {
            synchronized (object)
            {
                while(i<=19)
                {
                    if(i%3 ==1)
                    {
                        System.out.println("A");
                    }else if (i%3 ==2)
                    {
                        System.out.println("B");
                    }else 
                    {
                        System.out.println("C");
                    }  
                    i++;
                } 
            }
        }
    }
      

  6.   

    方法同步必须每个线程要使用同一个对象来锁
    你现在每个线程都会生成一个lock对象,肯定不对
    把以下:synchronized (lock) {
                if (i == 1)
                    System.out.println("A");
                if (i == 2)
                    System.out.println("B");
                if (i == 3)
                    System.out.println("C");
            }改成:synchronized (MutiThread.class) {
                if (i == 1)
                    System.out.println("A");
                if (i == 2)
                    System.out.println("B");
                if (i == 3)
                    System.out.println("C");
            }即可
      

  7.   

    题目没看仔细,不好意思哈
    如果要实现让线程按照一定的顺序,我的想法是,在外面定义一个int变量,通过这个变量来判断哪个线程应该等待,哪个线程应该执行,某个线程执行完之后,再把int变量设置成下个线程所对应的变量。
      

  8.   

    我写了一个,用文本编辑器写的,不知道有没有错误,楼主自己运行试试。
    class Printer implements Runnable{
    private static Object lock=new Object();
    private static int current=0; private String content=null;
    private int flag=0;

    public Printer(String content,int flag){
    this.content=content;
    this.flag=flag;
    }
    public void run{
    int times=0;
    while(times<19){
     synchronized (lock) {
    while(current%3!=flag){
    try{
    lock.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.print(content);
    times++;
    Runnable.current++;
    lock.notifyAll();
    }
    }
    }}
    public class ThreadTest
    {
        public static void main(String[] args)
        {
            new Thread(new Printer("A",0)).start();
    new Thread(new Printer("B",1)).start();
    new Thread(new Printer("C",2)).start();
        }
    }
      

  9.   

    晕,run方法定义没加括号……第12行改成 public void run(){
      

  10.   

    额 你咋知道new Thread(new Printer("A",0)).start();     new Thread(new Printer("B",1)).start();     new Thread(new Printer("C",2)).start这三个线程是按你写的顺序执行的。
      

  11.   


    class Printer implements Runnable {
    private static Object lock = new Object();
    private static int current = 0; private String content = null;
    private int flag = 0; public Printer(String content, int flag) {
    this.content = content;
    this.flag = flag;
    } public void run() {
    int times = 0;
    while (times < 19) {
    synchronized (lock) {
    while (current % 3 != flag) {
    try {
    lock.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.print(content);
    times++;
    Printer.current++;
    lock.notifyAll();
    }
    }
    }}public class ThreadTest {
    public static void main(String[] args) {
    new Thread(new Printer("A", 0)).start();
    new Thread(new Printer("B", 1)).start();
    new Thread(new Printer("C", 2)).start();
    }
    }12楼写的很好,十分感谢~结贴
      

  12.   

    current初始为0,只有第一个的flag是0满足条件current % 3 == flag
      

  13.   

    好吧 我稍微就看了一下 我把 content 看成了current
      

  14.   

    一段不使用synchronized关键字的代码,可以运行的,运行结果正确:class MyThread extends Thread{
        private static int i = 1;
        private final char[] CHARS = {'A', 'B', 'C'};
        private final int local_i;
        MyThread(int param){
           local_i = param;
        }
        public void run(){
           while(true){
               if(i == local_i){
                   System.out.println(CHARS[i - 1]);
                   i++;
                   break;
               }else{
                   try{
                       Thread.sleep(100);
                   }catch(Exception e){}
               }
           }
        }    public static void main(String[] args) {
            MyThread myThread1 = new MyThread(1);
            myThread1.start();
            MyThread myThread2 = new MyThread(2);
            myThread2.start();
            MyThread myThread3 = new MyThread(3);
            myThread3.start();
        }
    }