import java.awt.*;
public class Test {
public static void main(String[] args) {
Frame f = new Frame("thread");
f.setLayout(null);
f.setBounds(300,60,300,200); f.setVisible(true);
f.add(new MyTextArea(150,0,Thread.MIN_PRIORITY));
f.add(new MyTextArea(0,0,Thread.MAX_PRIORITY));
}
}
class MyTextArea extends TextArea implements Runnable{
MyTextArea(int x, int y, int priority){
setForeground(Color.BLUE);
setSize(150,200); setLocation(x,y);
Thread writer = new Thread(this);
writer.setPriority(priority);
writer.start();
}
public void run(){
for(int i = 1;i<=5000;i++){
append(i+"\n");
}
}
}
/////////////////////////////
Thread writer = new Thread(this);中的this代表什么呀?
最后的那个append();又是哪里来的呀?
public class Test {
public static void main(String[] args) {
Frame f = new Frame("thread");
f.setLayout(null);
f.setBounds(300,60,300,200); f.setVisible(true);
f.add(new MyTextArea(150,0,Thread.MIN_PRIORITY));
f.add(new MyTextArea(0,0,Thread.MAX_PRIORITY));
}
}
class MyTextArea extends TextArea implements Runnable{
MyTextArea(int x, int y, int priority){
setForeground(Color.BLUE);
setSize(150,200); setLocation(x,y);
Thread writer = new Thread(this);
writer.setPriority(priority);
writer.start();
}
public void run(){
for(int i = 1;i<=5000;i++){
append(i+"\n");
}
}
}
/////////////////////////////
Thread writer = new Thread(this);中的this代表什么呀?
最后的那个append();又是哪里来的呀?
TextArea可以appendthis表示正在运行的这个MyTextArea
Thread writer = new Thread(MyTextArea);
还是:
Thread writer = new Thread(MyTextArea(int x, int y, int priority)); 我真的很不明白?
哥哥别笑我,帮忙说个明白好吗?
你先看看这个,
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
这是你自己调用的那个在Thread类的一个构造方法的源代码,给你看看init(。。)方法的源代码,希望对你有帮助
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
} /* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
} /* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess(); /*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted(); this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
this.name = name.toCharArray();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext = AccessController.getContext();
this.target = target;
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize; /* Set thread ID */
tid = nextThreadID();
}
如果你认真看的话,就会发现这个:this.target = target;没错在thread中,有一个私有的属性:
/* What will be run. */
private Runnable target;
给出了这些,希望对你理解有帮助。
在Java中,this通常指当前对象,super则指父类的。当你想要引用当前对象的某种东西,比如当前对象的某个方法,或当前对象的某个成员,你便可以利用this来实现这个目的。
可能这样说比较好懂一些。
2.append是TextArea的一个方法!
意思是把消息追加到TextArea里面!