简体   繁体   中英

Variable transmission values for threading in java

I want to know how to transmit values to Thread. I want to Thread 1 show from 1-> 5 I want to Thread 2 show from 1-> 10

=>Through count variable.Please help me

public class NewClass {

    public static void main(String[] args) {
        MyThread myThread = new MyThread();

        myThread.setCount(10);
        Thread thread = new Thread(myThread);
        thread.start();

        myThread.setCount(5);
        Thread thread2 = new Thread(myThread);
        thread2.start();
    }

}

class MyThread implements Runnable {

    int count = 0;

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void run() {
        for (int i = 1; i <= count; i++) {
            System.out.println(Thread.currentThread().getName() + "\t\t" + i);
        }
    }
}

My idea is to split the list url and read the link I have them retrieved data to the database.It's too difficult for me please help Jsoup save content into the database

You should create 2 separate Thread objects, and set to each one the desired count.

MyThread t = new MyThread( );
t.setCount(10);
Thread t1 = new Thread(t);
t1.start( );

t = new MyThread( );
t.setCount(5);
Thread t2 = new Thread(t);
t2.start( );

In this way, each Thread object will run its own MyThread.run method, with the count you configured for each MyThread object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM