java多线程18章的简单介绍

博主:adminadmin 2023-01-10 14:39:09 821

本篇文章给大家谈谈java多线程18章,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用Java编写多线程

在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。

对于直接继承Thread的类来说,代码大致框架是:

?

123456789101112 class 类名 extends Thread{ 方法1; 方法2; … public void run(){ // other code… } 属性1; 属性2; … }

先看一个简单的例子:

?

12345678910111213141516171819202122232425262728 /** * @author Rollen-Holt 继承Thread类,直接调用run方法 * */class hello extends Thread { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i 5; i++) { System.out.println(name + "运行 " + i); } } public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.run(); h2.run(); } private String name; }

【运行结果】:

A运行 0

A运行 1

A运行 2

A运行 3

A运行 4

B运行 0

B运行 1

B运行 2

B运行 3

B运行 4

我们会发现这些都是顺序执行的,说明我们的调用方法不对,应该调用的是start()方法。

当我们把上面的主函数修改为如下所示的时候:

?

123456 public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.start(); h2.start(); }

然后运行程序,输出的可能的结果如下:

A运行 0

B运行 0

B运行 1

B运行 2

B运行 3

B运行 4

A运行 1

A运行 2

A运行 3

A运行 4

因为需要用到CPU的资源,所以每次的运行结果基本是都不一样的,呵呵。

注意:虽然我们在这里调用的是start()方法,但是实际上调用的还是run()方法的主体。

那么:为什么我们不能直接调用run()方法呢?

我的理解是:线程的运行需要本地操作系统的支持。

如果你查看start的源代码的时候,会发现:

?

1234567891011121314151617 public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0 || this != me) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } } private native void start0();

注意我用红色加粗的那一条语句,说明此处调用的是start0()。并且这个这个方法用了native关键字,次关键字表示调用本地操作系统的函数。因为多线程的实现需要本地操作系统的支持。

但是start方法重复调用的话,会出现java.lang.IllegalThreadStateException异常。

通过实现Runnable接口:

大致框架是:

?

123456789101112 class 类名 implements Runnable{ 方法1; 方法2; … public void run(){ // other code… } 属性1; 属性2; … }

来先看一个小例子吧:

?

123456789101112131415161718192021222324252627282930 /** * @author Rollen-Holt 实现Runnable接口 * */class hello implements Runnable { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i 5; i++) { System.out.println(name + "运行 " + i); } } public static void main(String[] args) { hello h1=new hello("线程A"); Thread demo= new Thread(h1); hello h2=new hello("线程B"); Thread demo1=new Thread(h2); demo.start(); demo1.start(); } private String name; }

【可能的运行结果】:

线程A运行 0

线程B运行 0

线程B运行 1

线程B运行 2

线程B运行 3

线程B运行 4

线程A运行 1

线程A运行 2

线程A运行 3

线程A运行 4

关于选择继承Thread还是实现Runnable接口?

其实Thread也是实现Runnable接口的:

?

12345678 class Thread implements Runnable { //… public void run() { if (target != null) { target.run(); } } }

其实Thread中的run方法调用的是Runnable接口的run方法。不知道大家发现没有,Thread和Runnable都实现了run方法,这种操作模式其实就是代理模式。关于代理模式,我曾经写过一个小例子呵呵,大家有兴趣的话可以看一下:

Thread和Runnable的区别:

如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。

?

1234567891011121314151617181920212223 /** * @author Rollen-Holt 继承Thread类,不能资源共享 * */class hello extends Thread { public void run() { for (int i = 0; i 7; i++) { if (count 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello h1 = new hello(); hello h2 = new hello(); hello h3 = new hello(); h1.start(); h2.start(); h3.start(); } private int count = 5; }

【运行结果】:

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

大家可以想象,如果这个是一个买票系统的话,如果count表示的是车票的数量的话,说明并没有实现资源的共享。

我们换为Runnable接口:

?

12345678910111213141516171819 /** * @author Rollen-Holt 继承Thread类,不能资源共享 * */class hello implements Runnable { public void run() { for (int i = 0; i 7; i++) { if (count 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello he=new hello(); new Thread(he).start(); } private int count = 5; }

【运行结果】:

count= 5

count= 4

count= 3

count= 2

count= 1

总结一下吧:

实现Runnable接口比继承Thread类所具有的优势:

1):适合多个相同的程序代码的线程去处理同一个资源

2):可以避免java中的单继承的限制

3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。

所以,本人建议大家劲量实现接口。

?

java 多线程

所要执行的指令,也包括了执行指令所需的系统资源,不同进程所占用的系统资源相对独立。所以进程是重量级的任务,它们之间的通信和转换都需要操作系统付出较大的开销。

线程是进程中的一个实体,是被系统独立调度和分派的基本单位。线程自己基本上不拥有系统资源,但它可以与同属一个进程的其他线程共享进程所拥有的全部资源。所以线程是轻量级的任务,它们之间的通信和转换只需要较小的系统开销。

Java支持多线程编程,因此用Java编写的应用程序可以同时执行多个任务。Java的多线程机制使用起来非常方便,用户只需关注程序细节的实现,而不用担心后台的多任务系统。

Java语言里,线程表现为线程类。Thread线程类封装了所有需要的线程操作控制。在设计程序时,必须很清晰地区分开线程对象和运行线程,可以将线程对象看作是运行线程的控制面板。在线程对象里有很多方法来控制一个线程是否运行,睡眠,挂起或停止。线程类是控制线程行为的唯一的手段。一旦一个Java程序启动后,就已经有一个线程在运行。可通过调用Thread.currentThread方法来查看当前运行的是哪一个线程。

class ThreadTest{

public static void main(String args[]){

Thread t = Thread.currentThread();

t.setName("单线程"); //对线程取名为"单线程"

t.setPriority(8);

//设置线程优先级为8,最高为10,最低为1,默认为5

System.out.println("The running thread: " + t);

// 显示线程信息

try{

for(int i=0;i3;i++){

System.out.println("Sleep time " + i);

Thread.sleep(100); // 睡眠100毫秒

}

}catch(InterruptedException e){// 捕获异常

System.out.println("thread has wrong");

}

}

}

多线程的实现方法

继承Thread类

可通过继承Thread类并重写其中的run()方法来定义线程体以实现线程的具体行为,然后创建该子类的对象以创建线程。

在继承Thread类的子类ThreadSubclassName中重写run()方法来定义线程体的一般格式为:

public class ThreadSubclassName extends Thread{

public ThreadSubclassName(){

..... // 编写子类的构造方法,可缺省

}

public void run(){

..... // 编写自己的线程代码

}

}

用定义的线程子类ThreadSubclassName创建线程对象的一般格式为:

ThreadSubclassName ThreadObject =

new ThreadSubclassName();

然后,就可启动该线程对象表示的线程:

ThreadObject.start(); //启动线程

应用继承类Thread的方法实现多线程的程序。本程序创建了三个单独的线程,它们分别打印自己的“Hello World!”。

class ThreadDemo extends Thread{

private String whoami;

private int delay;

public ThreadDemo(String s,int d){

whoami=s;

delay=d;

}

public void run(){

try{

sleep(delay);

}catch(InterruptedException e)

System.out.println("Hello World!" + whoami

+ " " + delay);

}

}

public class MultiThread{

public static void main(String args[]){

ThreadDemo t1,t2,t3;

t1 = new ThreadDemo("Thread1",

(int)(Math.random()*2000));

t2 = new ThreadDemo("Thread2",

(int)(Math.random()*2000));

t3 = new ThreadDemo("Thread3",

(int)(Math.random()*2000));

t1.start();

t2.start();

t3.start();

}

}

实现Runnable接口

编写多线程程序的另一种的方法是实现Runnable接口。在一个类中实现Runnable接口(以后称实现Runnable接口的类为Runnable类),并在该类中定义run()方法,然后用带有Runnable参数的Thread类构造方法创建线程。

创建线程对象可用下面的两个步骤来完成:

(1)生成Runnable类ClassName的对象

ClassName RunnableObject = new ClassName();

(2)用带有Runnable参数的Thread类构造方法创建线程对象。新创建的线程的指针将指向Runnable类的实例。用该Runnable类的实例为线程提供 run()方法---线程体。

Thread ThreadObject = new Thread(RunnableObject);

然后,就可启动线程对象ThreadObject表示的线程:

ThreadObject.start();

在Thread类中带有Runnable接口的构造方法有:

public Thread(Runnable target);

public Thread(Runnable target, String name);

public Thread(String name);

public Thread(ThreadGroup group,Runnable target);

public Thread(ThreadGroup group,Runnable target,

String name);

其中,参数Runnable target表示该线程执行时运行target的run()方法,String name以指定名字构造线程,ThreadGroup group表示创建线程组。

用Runnable接口实现的多线程。

class TwoThread implements Runnable{

TwoThread(){

Thread t1 = Thread.currentThread();

t1.setName("第一主线程");

System.out.println("正在运行的线程: " + t1);

Thread t2 = new Thread(this,"第二线程");

System.out.println("创建第二线程");

t2.start();

try{

System.out.println("第一线程休眠");

Thread.sleep(3000);

}catch(InterruptedException e){

System.out.println("第一线程有错");

}

System.out.println("第一线程退出");

}

public void run(){

try{

for(int i = 0;i 5;i++){

System.out.println(“第二线程的休眠时间:”

+ i);

Thread.sleep(1000);

}

}catch(InterruptedException e){

System.out.println("线程有错");

}

System.out.println("第二线程退出");

}

public static void main(String args[]){

new TwoThread();

}

}

程序运行结果如下:

正在运行的线程: Thread[第一主线程,5,main

创建第二线程

第一线程休眠

第二线程的休眠时间:0

第二线程的休眠时间:1

第二线程的休眠时间:2

第一线程退出

第二线程的休眠时间:3

第二线程的休眠时间:4

第二线程退出

另外,团IDC网上有许多产品团购,便宜有口碑

JAVA编程思想一共有几章

JAVA编程思想总共 22 个章节 你可以下载pdf查看

第1章 对象导论

第2章 一切都是对象

第3章 操作符

第4章 控制执行流程

第5章 初始化与清理

第6章 访问权限控制

第7章 复用类

第8章 多态

第9章 接口

第10章 内部类

第11章 持有对象

第12章 通过异常处理错误

第13章 字符串

第14章 类型信息

第15章 泛型

第16章 数组

第17章 容器深入研究

第18章 Java I/O系统

第19章 枚举类型

第20章 注解

第21章 并发

第22章 图形化用户界面

java多线程

Thread.start()方法会启动一个新的线程并在该线程上开始执行该Thread类构造方法中传递的那个Runnable对象的run方法。此时run方法运行于该新线程上,而线程名为"线程",因此run方法中输出"线程"。而在main方法中手动地去调用run方法并不会创建并启动一个新的线程,此时run方法与普通方法没有区别,都运行于调用它的线程上,run方法是从main线程中调用的因此run方法中输出"main"。

java多线程18章的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java多线程18章的信息别忘了在本站进行查找喔。