Java 使用多线程顺序打印语句
我正在尝试使用三个线程打印从1到10的数字。线程1打印1,2打印2,3打印3,4由线程1再次打印,依此类推 我已经创建了一个共享打印机资源,帮助这些线程打印数字。但我感到困惑的是,如何让所有线程都能看到这个数字 问题是每个线程都看到自己的数字副本,而我需要所有线程共享相同的数字 我试图创建这个示例以供学习。我曾在其他网页上看到过类似的问题,但我无法理解这一概念 感谢您的帮助 这个例子和我正在做的有什么不同?Java 使用多线程顺序打印语句,java,multithreading,concurrency,java.util.concurrent,Java,Multithreading,Concurrency,Java.util.concurrent,我正在尝试使用三个线程打印从1到10的数字。线程1打印1,2打印2,3打印3,4由线程1再次打印,依此类推 我已经创建了一个共享打印机资源,帮助这些线程打印数字。但我感到困惑的是,如何让所有线程都能看到这个数字 问题是每个线程都看到自己的数字副本,而我需要所有线程共享相同的数字 我试图创建这个示例以供学习。我曾在其他网页上看到过类似的问题,但我无法理解这一概念 感谢您的帮助 这个例子和我正在做的有什么不同? 公共类printAlternateEnumber{ 公共静态void main(字符串
公共类printAlternateEnumber{
公共静态void main(字符串参数[]){
SharedPrinter=新的SharedPrinter();
线程t1=新线程(新的myRunnable2(打印机,10,1),“1”);
线程t2=新线程(新myRunnable2(打印机,10,2),“2”);
线程t3=新线程(新myRunnable2(打印机,10,3),“3”);
t1.start();
t2.start();
t3.start();
}
}
类myRunnable2实现Runnable{
int max;
共享打印机;
int线程数;
整数=1;
myRunnable2(共享打印机,int max,int threadNumber){
这个。max=max;
this.printer=打印机;
this.threadNumber=threadNumber;
}
@凌驾
公开募捐{
System.out.println(“刚刚进入run的线程”+thread.currentThread().getName());
对于(inti=1;i我希望我理解正确,但是java中有一些主要的“特性”可以使变量在线程之间共享:
- volatile关键字
可变整数=1
- AtomicInteger(标准java类->无库)
AtomicInteger编号=新的AtomicInteger(1)
这两种技术都应该做你想做的,但是我没有使用它的经验,我只是想到了这个词,不知道它的意思,并做了一些挖掘
有些东西要读:;)
volatile for java解释-->
一个更好的解释(用图片!!)但是对于c#(仍然是相同的用法)-->
还有一个指向AtomicInteger的一些用法的链接-->
我希望我能帮助你,或者至少帮你找到正确的方向:)
-超级模糊请在这里查看我的解决方案
使用简单的等待/通知
使用循环屏障:
对于“它与偶数/奇数线程问题有多大区别”的查询。
-->这几乎是一样的…不是保持两个状态,而是多了一个状态来调用第三个线程,所以我相信,这可以扩展到任意数量的线程
编辑:
当您希望有“n”个线程按顺序执行工作时,可以查看此方法。(而不是使用不同的类t1、t2、t3等)
编辑2:
为上述解决方案再次在此处复制代码
我试图用一个类“Thrd”来解决这个问题,这个类用它的起始编号初始化
ThreadConfig类,它作为要创建的线程总数的大小
维护上一个线程状态的状态类。(用于维护顺序)
给你……(请回顾并让我知道你的观点)
编辑:
工作原理-->
当一个线程Tx有机会执行时,它将用x设置状态变量的状态。因此,下一个等待的线程(Tx+1)将在状态更新后有机会执行。这样可以保持线程的顺序
我希望我能够解释代码。请运行它,查看或让我知道以下代码的任何具体查询
(一)
包com.kalyan.concurrency
public class ThreadConfig {
public static final int size = 5;
}
public class State {
private volatile int state ;
public State() {
this.state =3;
}
public State(int state) {
this.state = state;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
public class Thrd implements Runnable {
int i ;
int name;
int prevThread;
State s;
public Thrd(int i,State s) {
this.i=i;
this.name=i;
this.prevThread=i-1;
if(prevThread == 0) prevThread=ThreadConfig.size;
this.s=s;
}
@Override
public void run() {
while(i<50)
{
synchronized(s)
{
while(s.getState() != prevThread)
{
try {
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized(s)
{
//if(s.getState() ==3)
if(s.getState()==prevThread)
System.out.println("t"+ name+ i);
s.setState(name);
i = i +ThreadConfig.size ;
s.notifyAll();
}
}
}
}
public class T1t2t3 {
public static void main(String[] args) {
State s = new State(ThreadConfig.size);
for(int i=1;i<=ThreadConfig.size;i++)
{
Thread T = new Thread(new Thrd(i,s));
T.start();
}
}
}
2) 包com.kalyan.concurrency
public class ThreadConfig {
public static final int size = 5;
}
public class State {
private volatile int state ;
public State() {
this.state =3;
}
public State(int state) {
this.state = state;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
public class Thrd implements Runnable {
int i ;
int name;
int prevThread;
State s;
public Thrd(int i,State s) {
this.i=i;
this.name=i;
this.prevThread=i-1;
if(prevThread == 0) prevThread=ThreadConfig.size;
this.s=s;
}
@Override
public void run() {
while(i<50)
{
synchronized(s)
{
while(s.getState() != prevThread)
{
try {
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized(s)
{
//if(s.getState() ==3)
if(s.getState()==prevThread)
System.out.println("t"+ name+ i);
s.setState(name);
i = i +ThreadConfig.size ;
s.notifyAll();
}
}
}
}
public class T1t2t3 {
public static void main(String[] args) {
State s = new State(ThreadConfig.size);
for(int i=1;i<=ThreadConfig.size;i++)
{
Thread T = new Thread(new Thrd(i,s));
T.start();
}
}
}
3) 包com.kalyan.concurrency
public class ThreadConfig {
public static final int size = 5;
}
public class State {
private volatile int state ;
public State() {
this.state =3;
}
public State(int state) {
this.state = state;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
public class Thrd implements Runnable {
int i ;
int name;
int prevThread;
State s;
public Thrd(int i,State s) {
this.i=i;
this.name=i;
this.prevThread=i-1;
if(prevThread == 0) prevThread=ThreadConfig.size;
this.s=s;
}
@Override
public void run() {
while(i<50)
{
synchronized(s)
{
while(s.getState() != prevThread)
{
try {
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized(s)
{
//if(s.getState() ==3)
if(s.getState()==prevThread)
System.out.println("t"+ name+ i);
s.setState(name);
i = i +ThreadConfig.size ;
s.notifyAll();
}
}
}
}
public class T1t2t3 {
public static void main(String[] args) {
State s = new State(ThreadConfig.size);
for(int i=1;i<=ThreadConfig.size;i++)
{
Thread T = new Thread(new Thrd(i,s));
T.start();
}
}
}
请将您的标题更改为描述该问题的内容。目前它没有任何用处。另外,请查看乒乓球多线程示例。这是同一个问题。@SotiriosDelimanolis:我更新了标题,我希望它现在看起来不错。我不明白如何将此数字作为共享资源打印?似乎每个线程都可以看到g它自己的副本。你能告诉我哪里做错了吗?我正在努力做这件事,花了大约4-5个小时,但没有取得任何进展:(Volatile不会导致变量在线程之间共享。我希望不使用AtomicInteger也能做到这一点