class Callme 
{
Thread current;
synchronized void call(String s)
{current=Thread.currentThread();
System.out.println("消息"+s);

System.out.println("消息"+s+"结束");
}
};
class Caller implements Runnable
{
String s="";
public Caller(String x)
{ s=x;
}
public void run()
{
Callme c=new Callme();
c.call(s); }
};
public class test
{
public static void main(String arg[])
{
try
{
String c1_s="1";
Thread w1=new Thread(new Caller(c1_s));
w1.start();
Thread.sleep(1000);
String c2_s="2";
Thread w2=new Thread(new Caller(c2_s));
w2.start();
Thread.sleep(1000);
String c3_s="3";
Thread w3=new Thread(new Caller(c3_s));
w3.start();
//c1.start();
//c2.start();
//c3.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}};你的错误真多

解决方案 »

  1.   

    你说的我试了一下不能实现,如果在那个同步的方法里sleep(),那这个线程就失去了那个类的锁了,其他线程就进入同步的类了啊
      

  2.   

    那还有什么办法没有呢?
    这是这道题的提示:
        创建一个类Callme,其中包含call方法,此方法输出消息后暂停一秒,然后输出“消息结束”;
        创建一个类Caller,这个类是一个线程类,要求继承Runnable接口,并实现run()方法。
        创建一个具有main()方法的类,此方法中将启动三个Caller线程
        使用同步机制控制三个Caller线程对象分别调用Callme对象的call方法。
      

  3.   

    public class testSynchro
    {
    public static void main(String arg[])
    {
    try
    {
    Thread w=new Thread();
    String c1_s="1";
    Caller c1=new Caller(w,c1_s);
    String c2_s="2";
    Caller c2=new Caller(w,c2_s);
    String c3_s="3";
    Caller c3=new Caller(w,c3_s);

    c1.start();
    c2.start();
    c3.start();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }};class Callme 
    {
    synchronized void call(Thread t,String s)
    {
    System.out.println("消息"+s);
    try
    {
    t.sleep(1000);
    }
    catch(Exception e)
    {}
    System.out.println("消息结束");
    }
    };
    class Caller implements Runnable
    {
    static Callme c=new Callme();
    Thread mine;
    Thread selfrun=new Thread(this);
    String s="";
    public Caller(Thread m,String x)
    {
    mine=m;
    s=x;
    }
    public void run()
    {
    c.call(mine,s); }

    public void start(){
    selfrun.start();
    }
    };这样你看行不。你的思路是很好的。
    你也可以把synchronized void call(Thread t,String s)换成一个静态方法,如下
    public class testSynchro
    {
    public static void main(String arg[])
    {
    try
    {
    Thread w=new Thread();
    String c1_s="1";
    Caller c1=new Caller(w,c1_s);
    String c2_s="2";
    Caller c2=new Caller(w,c2_s);
    String c3_s="3";
    Caller c3=new Caller(w,c3_s);

    c1.start();
    c2.start();
    c3.start();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }};class Callme 
    {
    synchronized static void  call(Thread t,String s)
    {
    System.out.println("消息"+s);
    try
    {
    t.sleep(1000);
    }
    catch(Exception e)
    {}
    System.out.println("消息结束");
    }
    };
    class Caller implements Runnable
    {
    Callme c;
    Thread mine;
    Thread selfrun=new Thread(this);
    String s="";
    public Caller(Thread m,String x)
    {
    c=new Callme();
    mine=m;
    s=x;
    }
    public void run()
    {
    c.call(mine,s); }

    public void start(){
    selfrun.start();
    }
    };
      

  4.   

    Callme c=new Callme();写在run()里面也可以上面的例2。
    给你解释一下。
    你的意思是想用一把锁吧。Thread mine=new Thread();在你的程序里面大概就是起了一把锁的作用。
    你的程序主要问题有两个。
    1。注意不是静态方法的锁,都是对象级的。也就是不同的对象有不同的锁,所以出现了前面的你们讨论的问题。
    2。扩展了Runnable的类,要运行还是要放进一个Thread的,不然无法运行的。然后强调一点,所有的静态方法锁的都是类级别的锁,所有有了第二种方法。
      

  5.   

    class Callme 
    {
    static Thread current;
    synchronized static void call(String s)//静态的就行了,刚才查了好久
    {
    current=Thread.currentThread();
    System.out.println("消息"+s);
    try {
    current.sleep(1000);
                      }
                      catch(Exception e) {}
    System.out.println("消息"+s+"结束");
    }
    };
    class Caller implements Runnable
    {
             String s="";
    public Caller(String x)
    { s=x;
    }
    public void run()
    {
    Callme.call(s);
    }
    };
    public class test
    {
    public static void main(String arg[])
    {
    try
    {
    String c1_s="1";
    Thread w1=new Thread(new Caller(c1_s));
    w1.start();
    String c2_s="2";
    Thread w2=new Thread(new Caller(c2_s));
    w2.start();
    String c3_s="3";
    Thread w3=new Thread(new Caller(c3_s));
    w3.start();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }};
      

  6.   

    不知道和你的初衷一样不一样,下面的代码可能好一点。
    public class testSynchro
    {
    public static void main(String arg[])
    {
    try
    {
    String c1_s="1";
    Caller c1=new Caller(c1_s);
    String c2_s="2";
    Caller c2=new Caller(c2_s);
    String c3_s="3";
    Caller c3=new Caller(c3_s);

    c1.start();
    c2.start();
    c3.start();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }};class Callme 
    {
    synchronized static void  call(String s)
    {
    System.out.println("消息"+s);
    try
    {
    Thread.sleep(1000);
    }
    catch(Exception e)
    {}
    System.out.println("消息结束");
    }
    };
    class Caller implements Runnable
    {
    Thread selfrun=new Thread(this);
    String s="";
    public Caller(String x)
    {
    s=x;
    }
    public void run()
    {
    Callme c=new Callme();
    c.call(s); }

    public void start(){
    selfrun.start();
    }
    };