谁能告诉我这个程序的功能是什么
来源:百度知道 编辑:UC知道 时间:2024/05/25 03:57:37
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 Dec();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.s
package com.lzy.interviewing; //导入包
public class ThreadTest1 {
private int j ;
public synchronized void inc() { //synchronized 线程互斥 同一时间内只能执行这个类//中的一个方法
j++;
System.out.println(Thread.currentThread().currentThread() + "This +j is :"
+ j); // currentThread() 返回对当前正在执行的线程对象的引用。currentThread()返回该//线程的名称。
}
public synchronized void dec() {
j--;
System.out.println(Thread.currentThread().getName() + "This -j is :"
+ j);
}
class Inc implements Runnable { //实现Runnable接口 java中有2中方法实现线程 1、扩展java.lang.Thread类 2、通过java.lang.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 metho