回答
这是另一种解决方案:
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 1; i <= 50; i += 2) {
System.out.println("T1=" + i);
t1turn = false;
try {
lock.notifyAll();
lock.wait();
} catch (InterruptedException e) {
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 2; i <= 50; i += 2) {
if (t1turn)
try {
lock.wait();
} catch (InterruptedException e) {
}
System.out.println("T2=" + i);
t1turn = true;
lock.notify();
}
}
}
});
t1.start();
t2.start();
可能是这仍然是相关的:
public class MyRunnable implements Runnable {
public static int counter = 0;
public static int turn = 0;
public static Object lock = new Object();
@Override
public void run() {
while (counter < 50) {
synchronized (lock) {
if (turn == 0) {
System.out.println(counter + " from thread "
+ Thread.currentThread().getName());
turn = 1;
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
turn = 0;
lock.notify();
}
}
}
}
}
,然后主要功能
public static void main(String[] args) {
Thread threadA = new Thread(new MyRunnable());
Thread threadB = new Thread(new MyRunnable());
threadA.start();
threadB.start();
}
public class PingPong extends Thread {
static StringBuilder object = new StringBuilder("");
public static void main(String[] args) throws InterruptedException {
Thread t1 = new PingPong();
Thread t2 = new PingPong();
t1.setName("\nping");
t2.setName(" pong");
t1.start();
t2.start();
}
@Override
public void run() {
working();
}
void working() {
while (true) {
synchronized (object) {
try {
System.out.print(Thread.currentThread().getName());
object.notify();
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
作业的本质是演示一个线程如何发出另一个线程的信号。最常用的方法是使用阻塞队列,但这里的信号不带任何信息,因此信号量就足够了。
创建其参数与2个信号灯线程类:输入和输出:
class ThreadPrinter implements Runnable {
int counter;
Semaphore ins, outs;
ThreadPrinter(int counter, Semaphore ins, Semaphore outs) {
this.counter = counter;
this.ins = ins;
this.outs = outs;
}
@Override
public void run() {
for (int i = 0; i < 25; i++) {
ins.aquire(); // wait for permission to run
System.out.println("" + counter);
outs.release(); // allow another thread to run
counter += 2;
}
}
创建2个Semaphore
S和它们传递到2个线程:
Semaphore a = new Semaphore(1); // first thread is allowed to run immediately
Semaphore b = new Semaphore(0); // second thread has to wait
ThreadPrinter tp1 = new ThreadPrinter(1, a, b);
ThreadPrinter tp2 = new ThreadPrinter(2, b, a);
注信号灯a
和b
被传递以不同的顺序。
这是工作完美。但是我很难理解它是如何工作的。你能解释一下run()方法执行2个线程吗?提前致谢。 – 2017-06-29 18:59:23
@PradeepSingh阅读描述信号量的任何教科书。优先考虑将信号量作为令牌容器的图片。 – 2017-06-29 23:05:18
public class Test {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 25; i++) {
synchronized (CommonUtil.mLock) {
incrementCount();
CommonUtil.mLock.notify();
try {
CommonUtil.mLock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 25; i++) {
synchronized (CommonUtil.mLock) {
incrementCount();
CommonUtil.mLock.notify();
try {
CommonUtil.mLock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
t1.start();
Thread.sleep(400);
t2.start();
t1.join();
t2.join();
}
private static void incrementCount() {
count++;
System.out.println("Count: " + count + " icnremented by: " + Thread.currentThread().getName());
}
}
class CommonUtil {
static final Object mLock = new Object();
}
public class ThreadCounter implements Runnable {
private static int count = 0;
private Thread t;
public ThreadCounter(String tName){
t= new Thread(this, tName);
t.start();
}
@Override
public void run() {
for(int i=1; i<=5; i++){
synchronized (CommonUtil.mLock) {
incrementCount(t.getName());
CommonUtil.mLock.notify();
try {
CommonUtil.mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void incrementCount(String tName){
System.out.println(tName+": "+(++ThreadCounter.count));
}
public static void main(String[] args) throws InterruptedException {
new ThreadCounter("Thread1");
Thread.sleep(500);
new ThreadCounter("Thread2");
}
}
class CommonUtil{
public static Object mLock = new Object();
}
我想这可能帮助。 虽然它不是标准的,但我希望它提供了一个更简单的方法。
public class ThreadDemo
{
public static void main (String [] args)
{
PrintDemo pd=new PrintDemo();
MyThread1 mt1 = new MyThread1 ("T1",pd);
MyThread2 mt2 = new MyThread2 ("T2",pd);
mt1.start();
mt2.start();
}
}
class PrintDemo {
private boolean oddFlag=true;
public synchronized void printOdd(int i,String tName){
if(oddFlag==false){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
System.out.println("\nThread "+tName+" count:"+i);
oddFlag=false;
notify();
}
}
public synchronized void printEven(int i,String tName){
if(oddFlag==true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
System.out.println("\nThread "+tName+" count:"+i);
oddFlag=true;
notify();
}
}
}
class MyThread1 extends Thread
{
private PrintDemo pd;
private String name;
MyThread1(String threadName,PrintDemo pd){
this.name=threadName;
this.pd=pd;
}
public void run()
{
for(int i=1;i<=50;i+=2){
pd.printOdd(i,name);
}
}
}
class MyThread2 extends Thread
{
private PrintDemo pd;
private String name;
MyThread2(String threadName,PrintDemo pd){
this.name=threadName;
this.pd=pd;
}
public void run()
{
for(int i=2;i<=50;i+=2){
pd.printEven(i,name);
}
}
}
我遇到同样的问题,预计所以我选择等待通知线程
public class Message implements Runnable {
private static final int N = 10;
private Thread thread;
private static Object object = new Object();
public Message(String name){
thread = new Thread(this, name);
thread.start();
}
public void run(){
for(int i=0; i<N; i++){
synchronized (object) {
System.out.println(i + "--" + thread.getName());
object.notify();
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
在main方法之间共享对象只使用基本知识:
Message message1 = new Message("Ping");
Message message2 = new Message("Pong");
这是最简单的解决方案,我能够想到。它使用同步方法并使用notify()和wait()来打印数字。希望能帮助到你。 :)
public class program implements Runnable
{
static int count =1;
private static final int MAX_COUNT = 50;
public synchronized void print()
{
System.out.println(Thread.currentThread().getName() + " is printing " + count);
count++;
notify();
try{
if(count>MAX_COUNT)
return;
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void run()
{
for(int i=0;i<MAX_COUNT/2;i++)
{
print();
}
}
public static void main(String[] args) {
program x= new program();
Thread t0= new Thread(x);
Thread t1= new Thread(x);
t0.start();
try
{
Thread.sleep(1);
} catch (InterruptedException e){
e.printStackTrace();
}
t1.start();
}
}
//simply use wait and notify and and set a counter and it will do
public class ThreadalternatePrint implements Runnable {
static int counter =0;
@Override
public synchronized void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(counter<51)
{ ++counter;
notify();
System.out.println(Thread.currentThread().getName());
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadalternatePrint obj1 = new ThreadalternatePrint();
Thread Th1 = new Thread(obj1);
Thread Th2 = new Thread(obj1);
Th1.setName("Thread1");
Th2.setName("Thread2");
Th1.start();
Th2.start();
}
}
你的anwser实际上是不正确的(打印线程名称而不是1,2,3,...),你可能想要解释一下你的代码,而不是仅仅倾销你的所有代码。 – glee8e 2017-07-01 06:05:29
@ glee8e感谢您的反馈,我会解释下面的代码。 以及它只是简单的线程方法实现,你可以简单地重命名为Thread的线程为1或2,并使用wait()并通知您可以通知()另一个线程并交替打印 您可以做的另一件事是创建一个如果条件与线程名称在运行条件。 – 2017-07-03 03:14:53
public class Testing implements Runnable {
private static int counter = 1;
private static final Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new Testing(), "1");
t1.start();
Thread t2 = new Thread(new Testing(), "2");
t2.start();
}
@Override
public void run() {
while (counter<=100) {
synchronized (lock) {
if (counter % 2 == 0) {
System.out.println(counter +" Written By Thread-"+ Thread.currentThread().getName());
counter++;
try {
lock.notifyAll();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (counter % 2 == 1) {
System.out.println(counter +" Written By Thread-"+ Thread.currentThread().getName());
counter++;
try {
lock.notifyAll();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
这里有相当多的代码。你应该考虑给出一个解释。 – ADyson 2017-08-11 12:33:59
- 1. 2个线程在顺序打印数
- 2. 编写一个从文件读取时间表并打印出来的程序?
- 3. 2个应用程序2个时区,我可以编程吗?
- 4. 是否可以编写一个程序来获取在线搜索结果?
- 5. 使用信号量从2个线程打印替代输出
- 6. 编写一个程序,在一个循环中请求一个整数用户到用户打印出0
- 7. 编写一个可以打开并使用其他程序的程序:(音频程序)
- 8. 如何编写一个程序来识别C中的转义序列并打印它们的值?
- 9. 你可以重写一个Publicnoncreatable程序来应用它的TLB吗?
- 10. 编写一个多线程C#应用程序
- 11. 用C和程序集编写一个简单的线程
- 12. 是否可以用C编写一个自毁程序?
- 13. 是否可以使用三种编程语言编写一个程序?
- 14. 如何编写这个clojure enlive程序,以便它可以解析多个url?
- 15. 使用pThreads,是否可以编写一个函数来检测它被调用的线程?
- 16. 如何编写一个python程序来打印出数据库中的一列记录
- 17. 两个线程可以使用相同的线程程序吗?
- 18. 这个程序为什么打印出“4 2 1 0”?
- 19. 如何编写一个C程序来执行另一个程序?
- 20. 如何编写一个程序来验证另一个程序/脚本?
- 21. 编译一个线程程序
- 22. log4net,可以将2个应用程序写入同一个日志文件吗?
- 23. 编写一个JVM字节码程序来计算并打印Fibonacci序列中的前20个数字
- 24. java:使用2个线程打印奇数偶数
- 25. 我可以编写一个开启/关闭USB的程序吗
- 26. 编写一个活着的线程
- 27. 您如何在Prolog中编写一个程序来使用递归来打印从1到10的数字?
- 28. 打印protovis输出(编程)
- 29. C编程。如何使方法来打印一个字符串
- 30. 我可以只使用NOSQL编写整个应用程序吗?
你知道如何编写一个程序,两个线程?如果没有,请参阅您最喜欢的线程教程。如果是这样,那么尝试编写这样一个程序,看看会发生什么。 – 2012-02-22 03:54:29
这是作业/作业吗? – 2012-02-22 03:54:37
顺便说一句,如果我在面试中问这个问题,我希望申请人会提到这是一个非常愚蠢的事情,试图用两个线程。作为理解线程API的测试并不差,但基本上要求两个线程以单线程方式运行。 – yshavit 2012-02-22 05:54:58