我想知道如何将值传输到线程。
我想从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 save content into the database
最佳答案
您应该创建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对象配置计数。