网站首页 > 文章中心 > 其它

java高级代码案例

作者:小编 更新时间:2023-08-12 13:32:04 浏览量:179人看过

求一个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 CallableInteger{

java高级代码案例-图1

public Calculate1000(){}

public Calculate1000(int a, int b){

this.a = a;

this.b = b;

}

int a;

int b;

java高级代码案例-图2

/**

* @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");

//异步

result = 0;

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多线程 编程题两个案例,不会写,求大神写出代码,万分感谢,多线程还没学.

/*

class A extends Thread

{

public void run()

try

Thread.sleep(1000);

}catch(Exception e)

System.out.println("A ERROR!");

System.out.println("AAAA");

class B extends Thread

System.out.println("B ERROR!");

System.out.println("BBBB");

class C extends Thread

java高级代码案例-图3

System.out.println("C ERROR!");

System.out.println("CCCC");

public class Test_1

public static void main(String[] args)

A a = new A();

B b = new B();

C c = new C();

a.start();

b.start();

c.start();

}*/

Thread ta = new Thread(a);

Thread tb = new Thread(b);

Thread tc = new Thread(c);

ta.start();

tb.start();

tc.start();

class A implements Runnable

class B implements Runnable

class C implements Runnable

案例一的两种方法已经写好;现在有事,晚上再把案例二代码写一下,应该没关系吧!

抱歉,是一个线程类,我看错了,晚上重发一下代码!

编写一个Java程序 要完整的代码

public class Compute{

public static void main(String args [])

Compute com1 = new Compute();

System.out.println(com1.mySqr(a));

System.out.println(com1.mySqr(b));

System.out.println(com1.mySqr(c));

public int mySqr(int x)

int value = x*x;

return value;

public float mySqr(float x)

float value = x*x;

public double mySqr(double x)

double value = x*x;

java线程经典代码

package threadgroup;

private String name;

private int delay;

name = sname;

delay = i_delay;

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

System.out.println("多线程测试!\n" + name + "\n" + delay);

public class testMyThread {

public static void main(String[] args) {

th1.start();

public class threadDemo {

Thread t = Thread.currentThread();

t.setName("土嘎嘎的粉丝们大家好吗?");

System.out.println("正在进行的Thread是:" + t);

System.out.println("我不叫穆继超" + i);

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

Thread t1 = Thread.currentThread();

t1.setName("第一个主进程");

System.out.println("正在运行" + t1);

System.out.println("在创建一个进程");

System.out.println("使他进入第一个睡眠状态");

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

System.out.println("进程" + i);

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

使用Java代码实现如下案例:+遍历1-100中的数字,求解1-100中所有偶数之和;+提?

int total = 0;

for(int i=1;i=100;i++) {

total = total+i;

System.out.println("1-100偶数之和为:"+total);

java代码示例

以上就是土嘎嘎小编为大家整理的java高级代码案例相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章