线程同步 - 几乎相同的表达式,但结果不同
public class MythreadM {
public static void main(String[] args) {
// TODO Auto-generated method stub
Msg m = new Msg();
Mythread thread1 = new Mythread(m);
thread1.start();
Mythread thread2 = new Mythread(m);
thread2.start();
Mythread thread3 = new Mythread(m);
thread3.start();
}
}
class Mythread extends Thread
{
private Msg m;
Mythread(Msg m)
{
this.m = m;
}
public void run()
{
System.out.println(getName()+"start");
for(int i=0; i<300; i++)
{
processMsg(i);
}
}
synchronized void processMsg(int i) // here starts question.
{
// synchronized(m)
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
}
class Msg
{
String str = "def message";
void print()
{
System.out.println(str+" "+System.currentTimeMillis());
}
void write(String str)
{
this.str = str;
}
}
上面的完整代码--------------------
遵循3个具有几乎相同表达式的代码。
&lt;&lt;代码1&gt;&gt; :同步(m){}
void processMsg(int i)
{
synchronized(m)
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
&lt;&lt;代码2&gt;&gt; :同步void processmsg(int i)
synchronized void processMsg(int i)
{
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
它看起来这些代码仅与不同的表达式相同,但作用不同。
&lt;代码2&gt;不同步,打印非同步结果。
为什么这会发生?睡眠(1);
的目的是什么? 提前致谢。
public class MythreadM {
public static void main(String[] args) {
// TODO Auto-generated method stub
Msg m = new Msg();
Mythread thread1 = new Mythread(m);
thread1.start();
Mythread thread2 = new Mythread(m);
thread2.start();
Mythread thread3 = new Mythread(m);
thread3.start();
}
}
class Mythread extends Thread
{
private Msg m;
Mythread(Msg m)
{
this.m = m;
}
public void run()
{
System.out.println(getName()+"start");
for(int i=0; i<300; i++)
{
processMsg(i);
}
}
synchronized void processMsg(int i) // here starts question.
{
// synchronized(m)
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
}
class Msg
{
String str = "def message";
void print()
{
System.out.println(str+" "+System.currentTimeMillis());
}
void write(String str)
{
this.str = str;
}
}
Full code above-----------------
and following 3 codes with almost same expressions.
<< code 1 >> : synchronized(m) {}
void processMsg(int i)
{
synchronized(m)
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
<< code 2 >> : synchronized void processMsg(int i)
synchronized void processMsg(int i)
{
{
m.write(getName()+" message "+i);
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.print();
}
}
It looks these codes mean same just with different expressions but acts different.
< code 2 > is not synchronized, prints non-synchronized results.
Why is this happening?
And what's the purpose of the sleep(1);
?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎这是示例代码,证明当在所有线程(M)上同步时,一次只能使用一个线程,而不是当您同步线程中的方法时,该线程不做任何事物,因为每个线程都会得到自己的锁。
在您的第二个问题上,睡眠的含义(1)是当前的线程应至少等待至少1毫秒1毫秒。
最后,根据我的经验,当您有几个线程以相同的资源(尤其是(print)类似System.Out)之类的线程时,解决此问题的最佳方法是要有一个消息队列以ynchronize ynchronize将消息传出。
It seems like this is example code to demonstrate that when synchronize on an object common to all threads (m), only one thread at a time can use it, as opposed to when you synchronize a method in the thread, which doesn't do anything because each thread is getting it's own lock.
To your second question, the meaning of sleep(1) is that the current thread should wait for at least 1 millisecond before proceeding.
Finally, in my experience, when you have several threads that are after the same resource (especially a (Print)stream like System.out) that the best way to solve this is to have a message queue to ynchronize the messages going out.