package com.Thread;public class NewThread implements Runnable{
Thread t ;
public NewThread(){
t = new Thread(this,"Thread Demo");
System.out.println("Child Thread:" + t);
t.start();
} public void run() {
try{
for(int i = 0; i <5; i ++){
System.out.println("Chile Thread:" + i );
Thread.sleep(500);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Exit Childe Thread");
} }class ThreadDemo{
public static void main(String args[]){
new NewThread();
try{
for(int i = 0 ; i < 5; i ++){
System.out.println("Main Thread:" + i );
hread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Thread t ;
public NewThread(){
t = new Thread(this,"Thread Demo");
System.out.println("Child Thread:" + t);
t.start();
} public void run() {
try{
for(int i = 0; i <5; i ++){
System.out.println("Chile Thread:" + i );
Thread.sleep(500);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Exit Childe Thread");
} }class ThreadDemo{
public static void main(String args[]){
new NewThread();
try{
for(int i = 0 ; i < 5; i ++){
System.out.println("Main Thread:" + i );
hread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
解决方案 »
- 求教 怎么用java把汉字编码成16进制字符串
- java.lang.NullPointerException
- 这是什么原因???
- 如何让一个线程给另外一个线程计时?
- JTextArea又一个问题希望高手耐心解答^^!
- 请教原代码:
- 请教tomcat配置经验,免up.
- 有大量jar包的时候报错:java.lang.OutOfMemoryError: PermGen space
- java中如何连接数据库?
- JB4安装完。运行时出现Failed to start Java VM:2 。怎么办!系统WIN2000pro。
- mouseEntered一个按钮的时候如何让他自动显示按钮的注释信息在一个小的注释框中??
- 有个数据传输的问题困扰小弟很久,今天贴出来,请各位达人指点。
NewThread的ctor/*构造函数*/
Thread的ctor,传递的是this,this是一个Runnbale的实现类对象.
start以上是在主线程的,而在start之后,是另外一个线程的运作了:
run() 跑~嗯,够了吧?
很想补充点JAVA线程的知识!
这个程序 new NewThread();后,跑到其构造方法里,其构造方法是真正建立线程和启动新建线程的地方.当t.start()后会去跑RUN方法.
RUN方法结束后只能说这个线程结束,并不代表程序结束,一个程序可能有好几个线程
public NewThread(){
t = new Thread(this,"Thread Demo");
System.out.println("Child Thread:" + t);
t.start();
}
这个程序块先执行,因为它是构造函数,而着里面的this指的是main函数。而这个构造函数里面又调用了t.start(),个人觉得下来执行的是run方法,最后执行:
try{
for(int i = 0 ; i < 5; i ++){
System.out.println("Main Thread:" + i );
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
===================================================================================
我把程序修改了下:class NewThread implements Runnable{
Thread t ;
public NewThread(){
t = new Thread(this,"Thread Demo");
System.out.println("Child Thread:" + t);
t.start();
} public void run() {
try{
for(int i = 0; i <5; i ++){
System.out.println("Chile Thread:" + i );
Thread.sleep(500);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Exit Childe Thread");
} }public class ThreadDemo
{
public static void main(String args[])throws Exception
{
Runnable run= new NewThread();Thread tt=new Thread(run);tt.join();
try{
for(int i = 0 ; i < 5; i ++)
{
System.out.println("Main Thread:" + i );
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
想看看join方法对其的影响,个人对join方法不怎么理解,发现每次运行结果不完全相同。还是有些不怎么明白,建议楼主看看thinking in java里面的初始化顺序的篇幅。