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{
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");
//异步
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;
楼主你试一下这段代码行不行,行的话请采纳!
/*
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
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
案例一的两种方法已经写好;现在有事,晚上再把案例二代码写一下,应该没关系吧!
抱歉,是一个线程类,我看错了,晚上重发一下代码!
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;
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("退出第二个进程");
int total = 0;
for(int i=1;i=100;i++) {
total = total+i;
System.out.println("1-100偶数之和为:"+total);
以上就是土嘎嘎小编为大家整理的java高级代码案例相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!