java用多线程实现累加求和
如何JAVA使用多线程实现累加求和,麻烦给一个代码比如说从1加到100,创建10个线程,第一个从1加到10,第2个从11加到20...线程同步执行,最后再把十个线程结果相...
如何JAVA使用多线程实现累加求和,麻烦给一个代码
比如说从1加到100,创建10个线程,第一个从1加到10,第2个从11加到20...线程同步执行,最后再把十个线程结果相加 展开
比如说从1加到100,创建10个线程,第一个从1加到10,第2个从11加到20...线程同步执行,最后再把十个线程结果相加 展开
5个回答
展开全部
在楼上基础上大概改一下,增加同步处理。
public class Test extends Thread {
static int n = 0;
private int startNum = 0 ;
public Test (int sn)
{
this.startNum = sn ;
}
public static synchronized void addSum (int num)
{
n += num ;
}
public static void main(String[] args) {
Thread [] thList = new Thread [10] ;
for (int i = 0; i < 10; i ++) {
thList [i] = new Test(i * 10 + 1) ;
thList [i].start();
}
for (int i = 0 ; i < 10 ; i ++)
{
thList [i].join () ;
}
System.out.println ("Sum is : " + n) ;
}
public void run() {
int sum = 0 ;
for (int i = 0; i < 10; ++i) {
sum += sn + i ;
}
addSum (sum) ;
}
}
public class Test extends Thread {
static int n = 0;
private int startNum = 0 ;
public Test (int sn)
{
this.startNum = sn ;
}
public static synchronized void addSum (int num)
{
n += num ;
}
public static void main(String[] args) {
Thread [] thList = new Thread [10] ;
for (int i = 0; i < 10; i ++) {
thList [i] = new Test(i * 10 + 1) ;
thList [i].start();
}
for (int i = 0 ; i < 10 ; i ++)
{
thList [i].join () ;
}
System.out.println ("Sum is : " + n) ;
}
public void run() {
int sum = 0 ;
for (int i = 0; i < 10; ++i) {
sum += sn + i ;
}
addSum (sum) ;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Thread_test extends Thread
{
int number;
public static int sum;
public Thread_test(int n) //构造函数
{
number=n;
}
public static synchronized void add(int num){ //同步方法
sum += num;
}
public void run()
{
int count=0;
for(int i=0;i<10;i++)
{
count+=number+i;
}
System.out.println("The "+((int)number/10+1)+" time: "+count);
add(count);
}
}
public class Main{
public static void main(String args[]) {
Thread_test test[] = new Thread_test[10];
for (int i = 0; i < 10; i++) {
test[i] = new Thread_test(i*10+1);
test[i].start();
try {
test[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Total is "+Thread_test.sum);
}
}
{
int number;
public static int sum;
public Thread_test(int n) //构造函数
{
number=n;
}
public static synchronized void add(int num){ //同步方法
sum += num;
}
public void run()
{
int count=0;
for(int i=0;i<10;i++)
{
count+=number+i;
}
System.out.println("The "+((int)number/10+1)+" time: "+count);
add(count);
}
}
public class Main{
public static void main(String args[]) {
Thread_test test[] = new Thread_test[10];
for (int i = 0; i < 10; i++) {
test[i] = new Thread_test(i*10+1);
test[i].start();
try {
test[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Total is "+Thread_test.sum);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test extends Thread {
static int n = 0;
public static void main(String[] args) {
for (int i = 0; i < 10; ++i) {
new Test().start();
}
}
public void run() {
for (int i = 0; i < 10; ++i) {
n++;
System.out.print(n + " ");
}
// System.out.print(n+" ");
}
}
static int n = 0;
public static void main(String[] args) {
for (int i = 0; i < 10; ++i) {
new Test().start();
}
}
public void run() {
for (int i = 0; i < 10; ++i) {
n++;
System.out.print(n + " ");
}
// System.out.print(n+" ");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Thread_Demo_1 {
public static int a ;
static int d;
public static void main(String[] args) {
Thread_Demo_1 tt = new Thread_Demo_1();
Thread_Demo_1.MyRunnable1 rt = tt.getmyrunnable();//实例化内部类(开辟空间)
tt.a = 1;
for (int i = 0; i < 4; i++)
{//start().启动线程
new Thread(rt,"任务"+i).start();
}
tt.d =1;
for (int i = 0; i < 8; i++)
{//计算20次累加的结果
d+=d;
}
System.out.println("累加8次的结果 = "+d);// d= 256
}
/*
* 内部类
*/
class MyRunnable1 implements Runnable{
int g =0;
@Override
public void run() {
int name = (int) Thread.currentThread().getId();
synchronized(this){
for (int i = 0; i <2; i++) {
++g;//累加的次数
Thread_Demo_1.a+=Thread_Demo_1.a;//实现累加
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//输出结果
System.out.println("Thread_Demo_1.a = "+Thread_Demo_1.a+"当前线程:"+name+"----累加了"+g+"次");
}
}
}
}
//获取内部类实例
public MyRunnable1 getmyrunnable() {
return new MyRunnable1();
}
}
public static int a ;
static int d;
public static void main(String[] args) {
Thread_Demo_1 tt = new Thread_Demo_1();
Thread_Demo_1.MyRunnable1 rt = tt.getmyrunnable();//实例化内部类(开辟空间)
tt.a = 1;
for (int i = 0; i < 4; i++)
{//start().启动线程
new Thread(rt,"任务"+i).start();
}
tt.d =1;
for (int i = 0; i < 8; i++)
{//计算20次累加的结果
d+=d;
}
System.out.println("累加8次的结果 = "+d);// d= 256
}
/*
* 内部类
*/
class MyRunnable1 implements Runnable{
int g =0;
@Override
public void run() {
int name = (int) Thread.currentThread().getId();
synchronized(this){
for (int i = 0; i <2; i++) {
++g;//累加的次数
Thread_Demo_1.a+=Thread_Demo_1.a;//实现累加
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//输出结果
System.out.println("Thread_Demo_1.a = "+Thread_Demo_1.a+"当前线程:"+name+"----累加了"+g+"次");
}
}
}
}
//获取内部类实例
public MyRunnable1 getmyrunnable() {
return new MyRunnable1();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己用thread写一个不就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询