thread_2.java
来自「java 线程 静态锁」· Java 代码 · 共 36 行
JAVA
36 行
package thread2;
class TestThread {
public void execute(){ //synchronized,未修饰
for(int i=0;i<20;i++){
try {
Thread.sleep(100); //比thread1 包中多出来的一句话1!!!!!!!!!!!!!!!!!!!!
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+Thread.currentThread().getName()+" "+i);
}
}
}
class ThreadA implements Runnable{
TestThread test=null;
public ThreadA(TestThread pTest){ //对象有外部引入,这样保证是同一个对象
test=pTest;
}
public void run() {
test.execute();
}
}
public class Thread_2{
public static void main(String[] args){
TestThread test=new TestThread();
Runnable runabble=new ThreadA(test);
Thread a=new Thread(runabble,"A");
a.start();
Thread b=new Thread(runabble,"B");
b.start();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?