我如何在 Java 中实现同步?
How can i Implement synchronized in Java?
我有一个关于多线程的作业,我需要一些帮助。
我有一个无法更改的资源class
public class Ressource {
public int val;
public void incr() {
val++;
}
public void decr() {
val--;
}
我有我的主要 Class
public class TwoThreads {
public static Ressource res = new Ressource();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 100; i++){
res.incr();
}
System.out.println(res.val);
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 100; i++){
res.decr();
}
System.out.println(res.val);
}
});
t1.start();
t2.start();
}
}
我尝试在我的 Override 方法中使用 synchronized,但它不起作用。我知道如果我使用
public synchronized void incr() {
val++;
}
它会起作用,但我不应该更改资源中的任何内容 Class。有什么想法吗?
i want to increase to 100 and then decrease to 0.First t1 should run
and when its finished should t2 start.But i have to do it in the main
method.
您可以按照以下方式进行:
t1.start();
t1.join();
t2.start();
演示:
class Ressource {
public int val;
public void incr() {
val++;
}
public void decr() {
val--;
}
}
public class Main {
public static Ressource res = new Ressource();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(res.val);
res.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(res.val);
res.decr();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(res.val);
}
});
t1.start();
t1.join();
t2.start();
}
}
我有一个关于多线程的作业,我需要一些帮助。
我有一个无法更改的资源class
public class Ressource {
public int val;
public void incr() {
val++;
}
public void decr() {
val--;
}
我有我的主要 Class
public class TwoThreads {
public static Ressource res = new Ressource();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 100; i++){
res.incr();
}
System.out.println(res.val);
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 100; i++){
res.decr();
}
System.out.println(res.val);
}
});
t1.start();
t2.start();
}
}
我尝试在我的 Override 方法中使用 synchronized,但它不起作用。我知道如果我使用
public synchronized void incr() {
val++;
}
它会起作用,但我不应该更改资源中的任何内容 Class。有什么想法吗?
i want to increase to 100 and then decrease to 0.First t1 should run and when its finished should t2 start.But i have to do it in the main method.
您可以按照以下方式进行:
t1.start();
t1.join();
t2.start();
演示:
class Ressource {
public int val;
public void incr() {
val++;
}
public void decr() {
val--;
}
}
public class Main {
public static Ressource res = new Ressource();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(res.val);
res.incr();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(res.val);
res.decr();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(res.val);
}
});
t1.start();
t1.join();
t2.start();
}
}