testthread1.java
来自「Threads为JAVA线程序例子集合.能方便了解JAVA线程的运作」· Java 代码 · 共 24 行
JAVA
24 行
class TestSynchronized implements Runnable{
static int j = 0;
public synchronized void run(){
for(int i=0; i<5; i++){ //(1)
System.out.println(Thread.currentThread().getName() + " : " + j++);
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println("Interrupted"); }
}
}
}
public class TestThread1{
public static void main(String[] args){
TestSynchronized r1 = new TestSynchronized();
TestSynchronized r2 = new TestSynchronized();
Thread t1 = new Thread(r1, "t1");
Thread t2 = new Thread(r2, "t2");
t1.start();
t2.start();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?