线程名字跟对象名字不是一个名字,this.name = name//表示你对象里有个name变量,当对象创建时构造函数将name变量赋值为传进来的参数String name.
给你发一下线程构造函数源代码(发几个构造函数给你),你看一下Thread构造函数的构造方法知道给线程起名需要调用 init()函数。代码如下:- public Thread() {
- init(null, null, "Thread-" + nextThreadNum(), 0);
- }
- /**
- * Allocates a new {@code Thread} object. This constructor has the same
- * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
- * {@code (null, target, gname)}, where {@code gname} is a newly generated
- * name. Automatically generated names are of the form
- * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
- *
- * @param target
- * the object whose {@code run} method is invoked when this thread
- * is started. If {@code null}, this classes {@code run} method does
- * nothing.
- */
- public Thread(Runnable target) {
- init(null, target, "Thread-" + nextThreadNum(), 0);
- }
- /**
- * Allocates a new {@code Thread} object. This constructor has the same
- * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
- * {@code (group, target, gname)} ,where {@code gname} is a newly generated
- * name. Automatically generated names are of the form
- * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
- *
- * @param group
- * the thread group. If {@code null} and there is a security
- * manager, the group is determined by {@linkplain
- * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
- * If there is not a security manager or {@code
- * SecurityManager.getThreadGroup()} returns {@code null}, the group
- * is set to the current thread's thread group.
- *
- * @param target
- * the object whose {@code run} method is invoked when this thread
- * is started. If {@code null}, this thread's run method is invoked.
- *
- * @throws SecurityException
- * if the current thread cannot create a thread in the specified
- * thread group
- */
- public Thread(ThreadGroup group, Runnable target) {
- init(group, target, "Thread-" + nextThreadNum(), 0);
- }
- /**
- * Allocates a new {@code Thread} object. This constructor has the same
- * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
- * {@code (null, null, name)}.
- *
- * @param name
- * the name of the new thread
- */
- public Thread(String name) {
- init(null, null, name, 0);
- }
复制代码 |