线程

Mikez 2004-03-17 10:47:06
不好意思问这么一个问题:
你们是怎么理解线程和进程的呢?
你们有没有这方面的书籍或者资源?
...全文
25 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mikez 2004-03-17
  • 打赏
  • 举报
回复
package threadpackage;
class NewThread implements Runnable{
Thread t;
NewThread(){
t = new Thread(this,"DemoThread");
System.out.println("child Thread: "+t);
t.start(); // start the thread
}

public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("Child Thread: "+i);
Thread.sleep(500);
}
} catch(InterruptedException e){
System.out.println("Child interrupted!");
}
System.out.println("Exit child thread.");
}
}

class ThreadDemo {
public static void main(String[] args) {
new NewThread(); // create a new thread
try{
for(int i=5;i>0;i--){
System.out.println("main Thread: "+i);
Thread.sleep(1000);
}
} catch(InterruptedException e){
System.out.println("Main interrupted.");
}
System.out.println("Exit main thread!");
}
}

这是一个线程的例子,可以帮我解释一下它的运行机制吗?
linprince 2004-03-17
  • 打赏
  • 举报
回复

进程是程序的一次动态执行过程,有产生,发展和消亡的过程
线程是比进程更小的执行单元,一个进程在它的执行过程中可以产生多个线程,也有产生,发展和消亡的过程,
但线程在执行过程中可以共享数据和内存,可以实现并发,提高系统的运行效率,这是线程与进程的最大不同
Mikez 2004-03-17
  • 打赏
  • 举报
回复
你能够再解释的详细一点吗?谢谢
FutureStonesoft 2004-03-17
  • 打赏
  • 举报
回复
进程是资源分配的单位,
线程是CPU调度的单位。

62,631

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧