关于Thread的一个问题,麻烦大家帮帮忙。
public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t2.start();
t1.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num = 0;
public synchronized void add(String name){
//synchronized (this) {
num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");
//}
}
}
test指向new出来的TestSync对象,在TestSync里面,作为成员变量,time引用指向新new出来的Timer对象,请问为什么会这样。
为什么Timer timer = new Timer();放在外面,
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);就是这一部分看不懂!!!!!!!!!!
而且已第一个类来new对象也很少见,王高手指点。
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t2.start();
t1.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num = 0;
public synchronized void add(String name){
//synchronized (this) {
num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");
//}
}
}
test指向new出来的TestSync对象,在TestSync里面,作为成员变量,time引用指向新new出来的Timer对象,请问为什么会这样。
为什么Timer timer = new Timer();放在外面,
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);就是这一部分看不懂!!!!!!!!!!
而且已第一个类来new对象也很少见,王高手指点。
...全文
相关推荐
RT-Thread常见问题
帮您解答RT-Thread操作系统开发中遇到的一些典型的问题
关闭线程Thread 解决进程中的问题
解决关闭主程序时,发现进程里总有一个.exe文件
RT-thread相关介绍与面试问题+答案
RT-thread相关介绍与秋招时面试问题以及相关答案 包含大量图片讲解 春招、秋招面试可用于巩固知识,不适合新手学习
阿里dubbo出错提示Thread pool is EXHAUSTED问题及解决方法
主要介绍了阿里dubbo出错提示Thread pool is EXHAUSTED的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
详解Java中Thread 和Runnable区别
主要介绍了Java中Thread 和Runnable的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


Java SE

