繁体   English   中英

java中线程的可变传输值

[英]Variable transmission values for threading in java

我想知道如何将值传递给Thread。 我想从1-> 5线程1显示我想从1-> 10线程2显示

=>通过计数变量。请帮帮我

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);
        }
    }
}

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

您应该创建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( );

这样,每个Thread对象都将运行自己的MyThread.run方法,并为每个MyThread对象配置计数。

暂无
暂无

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

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