public class Test09 { public static int k = 0; public static Test09 t1 = new Test09("t1"); public static Test09 t2 = new Test09("t2"); public static int i = print("i"); public static int n = 99; public int j = print("j"); { print("构造块"); } static{ print("静态块"); } public Test09(String str){ System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); ++n; ++i; } public static int print(String str){ System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); ++n; return ++i; } public static void main(String[] args) { } }
时间: 2024-01-28 20:03:36 浏览: 28
这段代码主要是用于测试Java中类的初始化顺序和静态代码块与构造代码块的执行顺序。代码中包含了静态变量、静态代码块、普通变量、构造代码块和构造方法,这些代码块和方法的执行顺序如下:
1. 静态变量和静态代码块的初始化顺序按照定义的顺序执行,即先执行`public static int k = 0;`,然后执行`public static Test09 t1 = new Test09("t1");`,再执行`public static Test09 t2 = new Test09("t2");`,最后执行`public static int i = print("i");`和静态代码块`static{ print("静态块"); }`。执行完毕后,静态变量`k`的值为4,静态变量`t1`和`t2`分别对应两个`Test09`对象的引用,静态变量`i`的值为3,静态变量`n`的值为100。
2. 执行`public Test09(String str)`构造方法时,先执行`{ print("构造块"); }`构造代码块,再执行构造方法中的代码。由于创建了两个`Test09`对象,因此构造方法和构造代码块各被执行了两次。执行完毕后,普通变量`j`的值为2。
3. 在`main`方法中没有代码,因此程序直接结束。
最终输出的结果为:
```
1:t1 i=1 n=99
2:构造块 i=2 n=100
3:t2 i=3 n=101
4:构造块 i=4 n=102
5:i i=5 n=103
6:静态块 i=6 n=104
```
相关问题
对于下列代码: public class Test{ int x; public Test (int x){ this.x = x; } public static void main(String args[]){ Test t1, t2, t3; t1 = new Test(5); t2 = new Test(5); t3 = t2; int n = 5; } } 下列哪些表达式返回值为true?() A. t1 == t2; B. t2 == t3; C. n == t1; D. t1.equals(n)
在这段代码中,t1和t2是两个不同的对象,它们的成员变量x的值相同,t3是指向t2对象的引用。n是一个int类型的变量,所以选项C和D都是不正确的。因此,只有选项A和B需要考虑。
A. t1 == t2;:这个表达式比较的是t1和t2两个对象的引用是否相等,即它们是否是同一个对象。由于t1和t2是两个不同的对象,因此它们的引用不相等,所以这个表达式的返回值为false。
B. t2 == t3;:这个表达式比较的是t2和t3两个对象的引用是否相等。由于t3是指向t2对象的引用,所以它们的引用相等,即它们指向同一个对象,因此这个表达式的返回值为true。
因此,正确的选项是B。
public class Test extends Thread { static int x=0; public static void a(){ x++; System.out.println(x); } public void run(){ a(); } public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->Test.a()); Thread t2=new Thread(()->Test.a()); Thread t3=new Thread(()->Test.a()); Thread t4=new Thread(()->Test.a()); Thread t5=new Thread(()->Test.a()); t1.start(); t1.join(); t2.start(); t2.join(); t3.start(); t3.join(); t4.start(); t4.join(); t5.start(); t5.join(); } }用信号量实现输出总为5
可以使用信号量来限制同时执行 `a()` 方法的线程数量,从而保证输出总为5。代码如下:
```
import java.util.concurrent.Semaphore;
public class Test extends Thread {
static int x = 0;
static Semaphore semaphore = new Semaphore(1);
public static void a() {
try {
semaphore.acquire();
x++;
System.out.println(x);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}
public void run() {
a();
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> Test.a());
Thread t2 = new Thread(() -> Test.a());
Thread t3 = new Thread(() -> Test.a());
Thread t4 = new Thread(() -> Test.a());
Thread t5 = new Thread(() -> Test.a());
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
}
}
```
在 `a()` 方法中,使用 `semaphore.acquire()` 申请信号量,如果已经有5个线程在执行,则会被阻塞,直到有一个线程执行完毕后释放信号量。同时在 `finally` 中释放信号量。这样就可以保证同时只有最多5个线程在执行 `a()` 方法,从而保证输出总为5。