加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 工程案例

求一个JAVA多线程例子,最好有代码,谢谢啦!

时间:2024-02-03 07:06:48  来源:http://www.gjqjd.com  作者:admin

求一个JAVA多线程例子,最好有代码,谢谢啦!

package a.b.test;

import java.util.Date;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class Calculate1000 implements Callable{ public Calculate1000(){} public Calculate1000(int a, int b){ this.a = a; this.b = b; } int a; int b; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //同步 Calculate1000 ca1 = new Calculate1000(); Date ds1 = new Date(); int result = 0; for(int i = 1 ; i <= 1000 ; i++){ result = ca1.add(i, result); } System.out.println(result); System.out.println(同步用时 + (new Date().getTime() - ds1.getTime()) + MS); //异步 Date ds2 = new Date(); result = 0; ExecutorService es = Executors.newFixedThreadPool(2); Future future1 = es.submit(new Calculate1000(1,500)); Future future2 = es.submit(new Calculate1000(501,1000)); result = future1.get() + future2.get(); System.out.println(result); System.out.println(异步用时 + (new Date().getTime() - ds2.getTime()) + MS); es.shutdown(); } private int add(int a, int b) throws Exception{ Thread.sleep(10); return a + b; } @Override public Integer call() throws Exception { int res = 0; for(int i = a ; i <= b ; i++){ res = this.add(res, i); } return res; } } 楼主你试一下这段代码行不行,行的话请采纳!

求Java高手写个程序(请参考图片中的实例)尽量跟着例子走··谢谢了

包名不能加汉字,你自己加合适的吧.....

interface CanFly {

public void Fly();

}

class Animal {

String name;

int age;

}

public class Bird extends Animal implements CanFly {

public Bird() {

this.name = 鹦鹉;

this.age = 3;

}

public Bird(String name, int age) {

this.name = name;

this.age = age;

}

public void Fly() {

System.out.println(Bird + name + is flying);

}

public static void main(String [] args) {

Bird b1 = new Bird(鸳鸯, 5);

Bird b2 = new Bird();

b1.Fly();

b2.Fly();

}

}

1. 在你的程序中,飞的接口名是:( CanFly )

2. 在你的程序中,父类名是:( Animal )

3. 在你的程序中,子类名是:( Bird )

4. 在你的程序中,主类名是:( Bird )

5. 在你的程序中,实现了的方法名是:( Fly )

6. 在你的程序中,程序开始执行的方法名是:( main )

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
    无相关信息
栏目更新
栏目热门