简体   繁体   English

java中线程的可变传输值

[英]Variable transmission values for threading in java

I want to know how to transmit values to Thread. 我想知道如何将值传递给Thread。 I want to Thread 1 show from 1-> 5 I want to Thread 2 show from 1-> 10 我想从1-> 5线程1显示我想从1-> 10线程2显示

=>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 我的想法是拆分列表网址并读取我将它们检索到的数据链接到数据库。这对我来说太难了请帮助Jsoup将内容保存到数据库中

You should create 2 separate Thread objects, and set to each one the desired count. 您应该创建2个单独的Thread对象,并将每个对象设置为所需的计数。

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. 这样,每个Thread对象都将运行自己的MyThread.run方法,并为每个MyThread对象配置计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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