编写10个线程,第一个线程从1加到10,第二个线程从11加到20 ..第十个线程从91加到100

Posted 王六六的IT日常

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写10个线程,第一个线程从1加到10,第二个线程从11加到20 ..第十个线程从91加到100相关的知识,希望对你有一定的参考价值。

不用syn关键字修饰

class ThreadTest extends Thread 
    int number;
    public ThreadTest(int n) 
        number=n;
    
    @Override
    public void run() 
        int count=0;
        for(int i=0;i<10;i++) 
            count+=number+i;
        

        System.out.println(number+"+"+(number+1)+"+...+"+(number+9)+"="+count);
    
    public static void main(String[] args) 
        for(int i=0;i<10;i++) 
            int k=i*10+1;
            ThreadTest tt=new ThreadTest(k);
            tt.start();
        
    

public class ThreadTest extends Thread 

    public static void main(String[] args) throws Exception 
        Thread[] threadList = new Thread[10];
        for (int i = 0; i < 10; i++) 
            threadList[i] = new ThreadTest(10 * i + 1);
            threadList[i].start();
        
        for (int i = 0; i < 10; i++) 
            threadList[i].join();
        
        System.out.println("十个线程和为:" + sum);
    

    private int stratNum;
    public static int sum;

    public ThreadTest(int startNum) 
        this.stratNum = startNum;
    
    public static void add(int num) 
        sum += num;
    
    public void run() 
        int sum = 0;
        for (int i = 0; i < 10; i++) 
            sum += stratNum + i;
        
        add(sum);
    



以上是关于编写10个线程,第一个线程从1加到10,第二个线程从11加到20 ..第十个线程从91加到100的主要内容,如果未能解决你的问题,请参考以下文章

JavaExp10:多线程设计,彩票购票问题

笔试题:创建三个线程,第一个线程打印1 2 3 4 5, 第二个线程打印 6 7 8 9 10...

java用多线程实现累加求和

Windows下用c++创建进程,再创建两个线程

Wait,Notify,Sleep 区别 新手看

C++ 套接字 Send() 线程安全