足球游戏_中国足彩网¥体育资讯$

java实现多线程总结
来源:易贤网 阅读:640 次 日期:2015-04-15 14:43:55
温馨提示:易贤网小编为您整理了“java实现多线程总结”,方便广大网友查阅!

在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。那么:为什么我们不能直接调用run()方法呢? 我的理解是:线程的运行需要本地操作系统的支持。 如果你查看start的源代码的时候,会发现:

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

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.start();

h2.start();

}

private String name;

}

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;

}

Thread和Runnable的区别:

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

class MyThread implements Runnable{

private int ticket = 5; //5张票

public void run() {

for (int i=0; i<=20; i++) {

if (this.ticket > 0) {

System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);

}

}

}

}

public class lzwCode {

public static void main(String [] args) {

MyThread my = new MyThread();

new Thread(my, "1号窗口").start();

new Thread(my, "2号窗口").start();

new Thread(my, "3号窗口").start();

}

}

class hello implements Runnable {

public void run() {

for(int i=0;i<10;++i){

synchronized (this) {

if(count>0){

try{

Thread.sleep(1000);

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.println(count--);

}

}

}

}

public static void main(String[] args) {

hello he=new hello();

Thread h1=new Thread(he);

Thread h2=new Thread(he);

Thread h3=new Thread(he);

h1.start();

h2.start();

h3.start();

}

private int count=5;

}

也可以采用同步方法。

语法格式为synchronized 方法返回类型方法名(参数列表){

// 其他代码

}

class hello implements Runnable {

public void run() {

for (int i = 0; i < 10; ++i) {

sale();

}

}

public synchronized void sale() {

if (count > 0) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(count--);

}

}

public static void main(String[] args) {

hello he = new hello();

Thread h1 = new Thread(he);

Thread h2 = new Thread(he);

Thread h3 = new Thread(he);

h1.start();

h2.start();

h3.start();

}

private int count = 5;

}

总结一下吧:

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

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

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

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

【使用线程同步解决问题】

采用同步的话,可以使用同步代码块和同步方法两种来完成。

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看CMS教程
易贤网手机网站地址:java实现多线程总结
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标