java编程题目:实现多线程累加,求代码1
发布网友
发布时间:2023-10-21 09:01
我来回答
共3个回答
热心网友
时间:2024-11-17 06:33
照着下面的例子改一下:
/**
* 实现Runnable接口的类
*
* @author leimin 2008-9-13 18:12:10
*/
publicclass DoSomethingimplements Runnable {
private String name;
public DoSomething(String name) {
this.name = name;
}
publicvoid run() {
for (int i = 0; i < 5; i++) {
for (long k = 0; k < 100000000; k++) ;
System.out.println(name + ": " + i);
}
}
}
/**
* 测试Runnable类实现的多线程程序
*
* @author leimin 2008-9-13 18:15:02
*/
publicclass TestRunnable {
publicstaticvoid main(String[] args) {
DoSomething ds1 = new DoSomething("阿三");
DoSomething ds2 = new DoSomething("李四");
Thread t1 = new Thread(ds1);
Thread t2 = new Thread(ds2);
t1.start();
t2.start();
}
}
执行结果:
李四: 0
阿三: 0
李四: 1
阿三: 1
李四: 2
李四: 3
阿三: 2
李四: 4
阿三: 3
阿三: 4
Process finished with exit code 0
2、扩展Thread类实现的多线程例子
/**
* 测试扩展Thread类实现的多线程程序
*
* @author leimin 2008-9-13 18:22:13
*/
publicclass TestThreadextends Thread{
public TestThread(String name) {
super(name);
}
publicvoid run() {
for(int i = 0;i<5;i++){
for(long k= 0; k <100000000;k++);
System.out.println(this.getName()+" :"+i);
}
}
publicstaticvoid main(String[] args) {
Thread t1 = new TestThread("阿三");
Thread t2 = new TestThread("李四");
t1.start();
t2.start();
}
}
执行结果:
阿三 :0
李四 :0
阿三 :1
李四 :1
阿三 :2
李四 :2
阿三 :3
阿三 :4
李四 :3
李四 :4
Process finished with exit code 0
热心网友
时间:2024-11-17 06:34
//
static int a,d =0;
public static void main(String[] args) {
MyRunnable4 myRunnable4 = new MyRunnable4();
//
for (int i = 0; i < 10; i++) {//启动线程
new Thread(myRunnable4,"任务:"+i).start();;
}
}
//run()要执行的方法 自增10次后相加
public static void show () throws InterruptedException{
for(int i=0;i<10;i++){
++Thread_Demo_3.d;//自增
Thread.sleep(500);
System.out.println("Thread_Demo_3.d = "+Thread_Demo_3.d+"---"+"当前线程:"+Thread.currentThread().getName());
}
Thread_Demo_3.a +=Thread_Demo_3.d;//自增10次后相加;
System.out.println(Thread_Demo_3.a+"----");
}
}
/*
* 实现Runnable类
*/
class MyRunnable4 implements Runnable{
@Override
public void run() {
try {
synchronized(this){//同步锁
Thread_Demo_3.show();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
热心网友
时间:2024-11-17 06:34
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableTest {
public static void main(String[] args) {
List<Callable<Integer>> list = new ArrayList<Callable<Integer>>();
for(int i = 0 ; i < 10 ; i ++){
final int x = i;
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int num = 0;
for (int j = x*10+1; j <= 10*(x+1); j++) {
num = num + j;
}
return num;
}
};
list.add(callable);
}
int sum = 0;
for (Callable<Integer> callable : list) {
FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
new Thread(futureTask).start();
try {
sum += futureTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("sum ->:" + sum);
}
}