Chapter 0 -- a new start

- Why you find this in CSDN blog?

For long i have been planning to start my own web log writing somewhere, while actually not. i admit i'm kind of lazy. After suveying several blog service providers i finally chose CSDN, mainly because it's a well-known domestic Internet corp. that's aiming for servicing "programmers".

- Why am i using English?

For practice ... i am not a native English speaker and would take every chance to practice my English. As a programmer i'm lucky to be able access many classic books on programming, and of coz, most of which are written in English.

- Why not Chapter 1?


In programming dialect the starting number is 0 -- one smaller than that of the others (which is 1). And i see this post as a new start to my blogging life ;-)


这代码是一个简单的多线程示例,包含一个主线程和两个自定义线程类,代码解释如下: ```java package chapter11; class MyThread2 extends Thread{ private String who; public MyThread2(String str) { who=str; } public void run() { for(int i=0;i<5;i++) { try { sleep((int)(1000*Math.random())); } catch(InterruptedException e) {} System.out.println(who+"正在运行!!"); } } } public class App11_3{ public static void main(String[] args) { MyThread2 you=new MyThread2("你"); MyThread2 she=new MyThread2("她"); you.start(); try{ you.join(); } catch(InterruptedException e) {} she.start(); try{ she.join(); } catch(InterruptedException e) {} System.out.println("主方法main()运行结束!"); } } ``` 1. 第一行代码 `package chapter11;` 声明了这个 Java 文件所在的包名为 chapter11。 2. `MyThread2` 类继承了 `Thread` 类,并实现了自己的构造函数和 `run()` 方法。其中,构造函数接收一个字符串参数 `str`,表示线程的名称;`run()` 方法内部使用一个循环来模拟线程执行任务,并在每次任务完成后打印一段信息。 3. `App11_3` 类是程序的主类,包含一个 `main()` 方法,用于启动自定义线程并等待其执行完毕。在 `main()` 方法内部,我们创建了两个 `MyThread2` 类的实例 `you` 和 `she`,分别传入不同的线程名称。 4. `you.start()` 启动了线程 `you`,此时线程进入就绪状态。 5. `you.join()` 让主线程等待线程 `you` 执行完毕后再继续执行,保证线程执行的顺序。 6. `she.start()` 启动了线程 `she`,此时线程进入就绪状态。 7. `she.join()` 让主线程等待线程 `she` 执行完毕后再继续执行,保证线程执行的顺序。 8. 最后一行输出一段信息,表示主线程执行完毕。 总的来说,这段代码演示了如何创建和启动多个线程,并保证它们按照一定的顺序执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值