用java多线程编程,输出"hello world"

来源:百度知道 编辑:UC知道 时间:2024/05/26 04:18:14
不会用多线程啊,
你这个是什么程序啊,看不懂啊.
麻烦解释一下

4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。
参考一下以下程序吧::
package com.lzy.interviewing;

public class ThreadTest1 {

private int j ;

public synchronized void inc() {
j++;
System.out.println(Thread.currentThread().getName() + "This +j is :"
+ j);
}

public synchronized void dec() {
j--;
System.out.println(Thread.currentThread().getName() + "This -j is :"
+ j);
}

class Inc implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
inc();
}
}
}

class Dec implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
dec();
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest1 tTest = new ThreadTest1();
Inc inc = tTest.new Inc();
Dec dec = tTest.new