Java 并排执行多线程
我想并行执行多个线程。Java 并排执行多线程,java,multithreading,Java,Multithreading,我想并行执行多个线程。 例如:将有一个简单的计数器方法,线程将访问该方法并打印计数器值。一个线程不应该在启动前等待另一个线程停止 示例输出[可能]: T1 1 t21 T1 2 T1 3 T1 4 t22 T1 5 我之前对多线程没有任何概念,只是想学习 没有实际问题 我认为您可以启动多个线程,让它们访问同步的printCounter方法 差不多 public class MyRunnable implemetns Runnable { private SharedObject o
例如:将有一个简单的计数器方法,线程将访问该方法并打印计数器值。一个线程不应该在启动前等待另一个线程停止 示例输出[可能]: T1 1
t21
T1 2
T1 3
T1 4
t22
T1 5
我之前对多线程没有任何概念,只是想学习 没有实际问题 我认为您可以启动多个线程,让它们访问同步的printCounter方法 差不多
public class MyRunnable implemetns Runnable {
private SharedObject o
public MyRunnable(SharedObject o) {
this.o = o;
}
public void run() {
o.printCounter();
}
}
然后开始你就可以
new Thread(new MyRunnable()).start();
new Thread(new MyRunnable()).start();
等
然后在sharedObject方法中,您希望有一个方法,该方法包含一个可以打印的变量。这种方法也可以增加计数器
请注意,线程调度程序不能保证线程何时运行。您还没有真正询问任何特定的问题。如果您只是在寻找跨两个或多个线程共享非线程安全计数器的一般示例,请执行以下操作:
public class Counter extends Thread {
private static int count = 0;
private static int increment() {
return ++count; //increment the counter and return the new value
}
@Override
public void run() {
for (int times = 0; times < 1000; times++) { //perform 1000 increments, then stop
System.out.println(increment()); //print the counter value
}
}
public static void main(String[] args) throws Exception {
new Counter().start(); //start the first thread
new Counter().start(); //start the second thread
Thread.sleep(10000); //sleep for a bit
}
}
公共类计数器扩展线程{
私有静态整数计数=0;
私有静态int增量(){
return++count;//递增计数器并返回新值
}
@凌驾
公开募捐{
对于(int times=0;times<1000;times++){//执行1000次增量,然后停止
System.out.println(increment());//打印计数器值
}
}
公共静态void main(字符串[]args)引发异常{
新计数器().start();//启动第一个线程
新计数器().start();//启动第二个线程
Thread.sleep(10000);//睡一会儿
}
}
如果计数器是共享的,则您需要以下内容:
class ThreadTask implements Runnable {
private static AtomicInteger counter = new AtomicInteger();
private int id;
public ThreadTask(int id) { this.id = id; }
public void run() {
int local = 0;
while((local = counter.incrementAndGet()) < 500) {
System.out.println("T" + id + " " + local);
}
}
}
...
new Thread(new ThreadTask(0)).start();
new Thread(new ThreadTask(1)).start();
class ThreadTask实现可运行{
私有静态AtomicInteger计数器=新的AtomicInteger();
私有int-id;
公共线程任务(int-id){this.id=id;}
公开募捐{
int local=0;
而((local=counter.incrementAndGet())<500){
System.out.println(“T”+id+“”+local);
}
}
}
...
新线程(新线程任务(0)).start();
新线程(新线程任务(1)).start();
否则,如果需要每线程计数器:
class ThreadTask implements Runnable {
private int counter = 0;
private int id;
public ThreadTask(int id) { this.id = id; }
public void run() {
while(counter < 500) {
counter++;
System.out.println("T" + id + " " + counter);
}
}
}
...
new Thread(new ThreadTask(0)).start();
new Thread(new ThreadTask(1)).start();
class ThreadTask实现可运行{
专用整数计数器=0;
私有int-id;
公共线程任务(int-id){this.id=id;}
公开募捐{
同时(计数器<500){
计数器++;
系统输出打印项次(“T”+id+“”+计数器);
}
}
}
...
新线程(新线程任务(0)).start();
新线程(新线程任务(1)).start();
您已经发布了一条“我想要”但还没有问题。你肯定先读过线程的教程,对吧?你的具体步骤是什么?并排开始线程。对于计数器1-500,优先级较高的线程应该比优先级较低的线程完成得更快。最好的学习方法是开始这样做。简单的多线程代码有数百万个示例,因此我将从其中一些开始。实际上不需要睡眠,因为它们不是守护进程线程。@Tudor-是的,但是当程序终止时,某些IDE可能会清除/隐藏控制台输出。这就是为什么有sleep()
的主要原因。在所有线程完成之前,程序不会终止。@PeterLawrey-是的,一个只打印1000行的线程最多只需几秒钟就可以完成。