想问下面这样的代码中thread1.a();  thread2.b();在主程序中能同步吗!?
class thread1 {
synchronized void a(){}
void run(){..}
.......
}
class thread2{
synchronized void b(){}
void run(){..}
.........
}
class ss{
public static void main(String[]  args){
   thread1 th1 = new thread1();
   thread2 th2 =  new thread2();
   th1.start();
   th2.start();
......
...}
}

解决方案 »

  1.   

    我想你得先搞清楚什么叫线程同步,
    还有你的THREAD1和2好想错了
      

  2.   

    线程同步的问题网上有很多代码可以参考。比如JDBMonitor中就是用线程同步实现的消息制造-消费的。
    比如:
    static class LogConsumer implements Runnable
    {
    public void run()
    {
    try
    {
    startConsumer();
    } catch (InterruptedException e)
    {
    throw CommonUtils.toRuntimeException(e);
    } catch (LoggerException e)
    {
    throw CommonUtils.toRuntimeException(e);
    }
    }

    private void startConsumer() throws LoggerException, InterruptedException
    {
    for (;;)
    {
    SQLInfo info = (SQLInfo) channel.take();
    for (int i = 0, n = dbListeners.length; i < n; i++)
    {
    dbListeners[i].logSql(info);
    } }
    }

    }
    在DBLogger类中,从www.cownew.com下载。
    本人欲成立一个java开源组织,有兴趣的朋友可以加入,开源组织设立两个区域,一个区域是实际开发区,主要是实际的项目的开发;一个区域是后备力量培养区,主要为有热情加入开源组织、有一定的开发经验的朋友准备,这个区域将由专门的专家指导,提高开发能力。我们将积极的联系开源项目商业化,让项目组成员得到适当的回报。
    有意者联系:qq:57077490 qq群:20392347