计数到10,但线程在开始之前等待其他线程
我想创建一个这样的程序: 我从这样的主要10个线程开始(我的类实现运行)
public class Main {
public static void main(String[] ar) {
for (int i = 1; i <= 5; i++) {
Count1by1 count1by1 = new Count1by1(i);
Thread myThread = new Thread(count1by1);
myThread.start();
}
}
}
,我想创建一个关键部分,如果该线程输入,则可以计数到10,如果没有,则会等待()。 我尝试了许多实现,但无法正常工作(因为每个线程都在不等待的情况下计算到10 ...
这是类
public class Count1by1 implements Runnable{
private int threadnumber;
private Object mutex = new Object();
public Count1by1(int num) {
this.threadnumber=num;
//this.mutex= new Object();
}
public void count() {
synchronized (mutex) {
for (int i = 1; i <= 10; i++) {
System.out.println("#"+threadnumber + " counts: " + i);
try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
@Override
public void run() {
count();
}
}
I would like to create a program like this:
I start in my main 10 threads like this (My class implements runnable)
public class Main {
public static void main(String[] ar) {
for (int i = 1; i <= 5; i++) {
Count1by1 count1by1 = new Count1by1(i);
Thread myThread = new Thread(count1by1);
myThread.start();
}
}
}
and I want to create a critical section that if the thread enters it can count to 10, if not it will wait().
I've tried many implementations but is not working (cuz every threads count to 10 without waiting...
This is the class
public class Count1by1 implements Runnable{
private int threadnumber;
private Object mutex = new Object();
public Count1by1(int num) {
this.threadnumber=num;
//this.mutex= new Object();
}
public void count() {
synchronized (mutex) {
for (int i = 1; i <= 10; i++) {
System.out.println("#"+threadnumber + " counts: " + i);
try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
@Override
public void run() {
count();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。您创建一个新对象。它没有名称(因为对象没有名称)。您还创建了一个字段(这不是对象;它是一个指针)。目前,它指向您制作的新对象。
count1by1的10个实例中的每个实例都有一个字段,并且每个点都指向一个唯一对象,因为它们都运行
new Object()
。好的,这将采用字段
mutex
遵循其指向的内容,在那里找到对象,然后锁定在其中。鉴于有10个唯一的对象(每个Count1by1实例都有其自己的对象),这一无所获。要有互斥X,至少需要2个线程锁定同一对象。解决方案
使主体中的锁定对象并将其传递到线程:
现在有一个静音对象(提示:计算代码执行
new
语句的次数,这就是您拥有的数量)。您的10个count1by1实例中的每一个都有其自己的领域,但它们都指向相同的物体(就像10个人有一张纸,上面写着相同的家庭地址:10个“变量”,只有一个房屋),因此,同步它们会做点什么。Okay. You create a new object. It has no name (because objects do not have names). You also created a field (which isn't an object; it's a pointer to one). It currently points at the new object you made.
Each of the 10 instances of Count1by1 has a field, and each points to a unique object, given that they all run
new Object()
.Okay, this takes the field
mutex
follows what its pointing at, finds the object there, and then locks on that. Given that there are 10 unique objects (each Count1by1 instance has its own object), this accomplishes nothing. To have a mutex, at least 2 threads need to lock on the same object.Solution
Make the lock object in your main and pass it to your threads:
Now there's one mutex object (hint: Count the number of times the code executes a
new
statement, that's how many you have). Each of your 10 instances of Count1by1 has its own field, but they are all pointing at the same object (it's like 10 people having a piece of paper with the same home address written on it: 10 'variables', just one house), hence, synchronizing on them will do something.