Java-不在多线程程序中循环
我还是一个java新手,正在尝试学习线程。我的问题是它不会循环5次。它运行一次并退出。我使用一个.class来锁定class对象,这样两个线程都锁定在同一个对象监视器上Java-不在多线程程序中循环,java,multithreading,Java,Multithreading,我还是一个java新手,正在尝试学习线程。我的问题是它不会循环5次。它运行一次并退出。我使用一个.class来锁定class对象,这样两个线程都锁定在同一个对象监视器上 class a implements Runnable { Thread thr; int count; String time; a(String s) { thr = new Thread(this, s); thr.start(); } public void run() {
class a implements Runnable {
Thread thr;
int count;
String time;
a(String s) {
thr = new Thread(this, s);
thr.start();
}
public void run() {
count++;
if (Thread.currentThread().getName().compareTo("one") == 0) {
synchronized (a.class) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);
time = "Tick";
System.out.println(time);
notify();
while (time == "Tock") {
wait();
}
}
} catch (Exception e) {
}
}
} else if (Thread.currentThread().getName().compareTo("two") == 0) {
synchronized (a.class) {
try {
for (int j = 0; j < 5; j++) {
System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);
time = "Tock";
System.out.println(time);
notify();
while (time == "Tick") {
wait();
}
}
} catch (Exception e) {
}
}
}
}
}
public class b {
public static void main(String args[]) {
a obj1 = new a("one");
a obj2 = new a("two");
}
}
a类实现可运行{
螺纹螺纹;
整数计数;
串时间;
a(字符串s){
thr=新螺纹(此,s);
thr.start();
}
公开募捐{
计数++;
if(Thread.currentThread().getName().compareTo(“一”)==0){
同步(a.class){
试一试{
对于(int i=0;i<5;i++){
System.out.println(“正在运行的线程”+thread.currentThread().getName()+”,带有count“+count);
time=“滴答”;
系统输出打印LN(时间);
通知();
而(时间=“Tock”){
等待();
}
}
}捕获(例外e){
}
}
}else if(Thread.currentThread().getName().compareTo(“两个”)==0){
同步(a.class){
试一试{
对于(int j=0;j<5;j++){
System.out.println(“正在运行的线程”+thread.currentThread().getName()+”,带有count“+count);
time=“Tock”;
系统输出打印LN(时间);
通知();
而(时间=“勾选”){
等待();
}
}
}捕获(例外e){
}
}
}
}
}
公共b级{
公共静态void main(字符串参数[]){
a obj1=新a(“一”);
a obj2=新的a(“两”);
}
}
在比较字符串(以及一般的对象)时,应该使用equals
,而不是=
(通常为原语保留):while(time.equals(“Tock”)
<当您想要(并且认为它应该)返回true
时,字符串上的code>=通常会导致false
,因此您的循环将在预期之前退出。为什么只循环一次的答案是调用notify()
在未锁定的对象上,因此会抛出一个IllegalMonitorStateException
,并被空的catch语句捕获
这是一种方法。不是说这是最好的。我试图让它接近您的代码:
public class TickTock {
static final int N = 4;
Object lock = new Object();
int token;
class Worker extends Thread {
int id;
Worker(int id) {
this.id = id;
}
@Override
public void run() {
try {
synchronized (lock) {
for (int i = 0; i < 5; i++) {
while (id != token%N) lock.wait();
System.out.println(id + " " + i);
token++;
lock.notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void start() {
for (int i = 0; i < N; i++) {
new Worker(i).start();
}
}
public static void main(String[] args) {
new TickTock().start();
}
}
公共类滴答滴答{
静态最终整数N=4;
对象锁=新对象();
int标记;
类工作者扩展线程{
int-id;
工作人员(内部id){
this.id=id;
}
@凌驾
公开募捐{
试一试{
已同步(锁定){
对于(int i=0;i<5;i++){
while(id!=令牌%N)lock.wait();
System.out.println(id+“”+i);
令牌++;
lock.notifyAll();
}
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
void start(){
对于(int i=0;i
给您,原始代码如下:
class a implements Runnable {
Thread thr;
int count;
static String time = "Tock";
a(String s) {
thr = new Thread(this, s);
thr.start();
}
public void run() {
count++;
if (Thread.currentThread().getName().compareTo("one") == 0) {
synchronized (a.class) {
try {
for (int i = 0; i < 5; i++) {
while (time.equals("Tock")) {
a.class.wait();
}
System.out.println("Now running thread "
+ Thread.currentThread().getName()
+ " with count " + count);
time = "Tock";
System.out.println(time);
a.class.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (Thread.currentThread().getName().compareTo("two") == 0) {
synchronized (a.class) {
try {
for (int j = 0; j < 5; j++) {
while (time.equals("Tick")) {
a.class.wait();
}
System.out.println("Now running thread "
+ Thread.currentThread().getName()
+ " with count " + count);
time = "Tick";
System.out.println(time);
a.class.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public class Test {
public static void main(String args[]) {
a obj1 = new a("one");
a obj2 = new a("two");
}
}
a类实现可运行{
螺纹螺纹;
整数计数;
静态字符串time=“Tock”;
a(字符串s){
thr=新螺纹(此,s);
thr.start();
}
公开募捐{
计数++;
if(Thread.currentThread().getName().compareTo(“一”)==0){
同步(a.class){
试一试{
对于(int i=0;i<5;i++){
而(时间等于(“Tock”)){
a、 class.wait();
}
System.out.println(“正在运行线程”
+Thread.currentThread().getName()
+“带计数”+计数);
time=“Tock”;
系统输出打印LN(时间);
a、 类通知();
}
}捕获(例外e){
e、 printStackTrace();
}
}
}else if(Thread.currentThread().getName().compareTo(“两个”)==0){
同步(a.class){
试一试{
对于(int j=0;j<5;j++){
while(time.equals(“勾选”)){
a、 class.wait();
}
System.out.println(“正在运行线程”
+Thread.currentThread().getName()
+“带计数”+计数);
time=“滴答”;
系统输出打印LN(时间);
a、 类通知();
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
}
}
公开课考试{
公共静态void main(字符串参数[]){
a obj1=新a(“一”);
a obj2=新的a(“两”);
}
}
问题是,当锁被保持在a.class
对象上时,您正在对隐式this
对象调用wait
和notify
,因此您必须在a.class
上调用wait/notify
。就这样
我还做了一个小的重组,因为我假设您希望他们以交替顺序打印
勾选
和勾选
,对吗?由于缺少缩进,您的代码很难阅读。请相应地编辑您的帖子。您还应该避免捕获(异常e){},因为它很可能隐藏了出错的地方。@Jon…您是对的,它实际上是在抛出一个非法的MonitorStateException。我不明白您想用它做什么