View Full Version : سوال: تبدیل به جاوا
taouna
پنج شنبه 22 اردیبهشت 1390, 22:19 عصر
using System;
using System.Threading;
public class Producer
{
private HoldBuffer sharedLocation;
private Random randomSleepTime;
public Producer( HoldBuffer shared, Random random )
{
sharedLocation = shared;
randomSleepTime = random;
} // end constructor
public void Produce()
{
for ( int count = 1; count <= 10; count++ )
{
Thread.Sleep( randomSleepTime.Next( 1, 3001 ) );
sharedLocation.Buffer = count;
} // end for
Console.WriteLine( "{0} done producing.\nTerminating {0}.",
Thread.CurrentThread.Name );
} // end method Produce
} // end class Producer
سلام کسی میتونه برای تبدیل این کلاس (سیشارپ)به جاوا کمکم کنه
ermia2008
پنج شنبه 22 اردیبهشت 1390, 23:19 عصر
سلام دوست عزیز
اولا اینکه خوش اومدید به سایت برنامه نویس
دوم اینکه مرد مومن خداییش یه نگاه به پستی که فرستادی بکن! از تگ کد حتما استفاده کن
سوم اینکه بگو جریان چیه و میخوای چی کار کنی که بهتر بشه راهنمایی کرد
Mbt925
جمعه 23 اردیبهشت 1390, 10:25 صبح
اینطور که معلومه مسئله تولید کننده-مصرف کننده به زبان جاوا رو می خواید! البته کدی که گذاشتید اگه بخواد به صورت موازی اجرا بشه، کد درستی نیست، چون نیاز به همزمانی داره!
public class Bus {
private String message;
private boolean empty = true;
public synchronized String take() {
while (empty) {
try { wait();
} catch (InterruptedException e) {} }
empty = true;
notifyAll();
return message;
}
public synchronized void put(String message) {
while (!empty) {
try { wait();
} catch (InterruptedException e) {} }
empty = false;
this.message = message;
notifyAll();
}
}
public class Producer implements Runnable {
private Bus bus;
public Producer(Bus bus) {
this.bus = bus;
}
public void run() {
String importantInfo[] = { “Message1", "Message2",
"Message3", "Message4" };
Random random = new Random();
for (int i = 0; i < importantInfo.length; i++) {
bus.put(importantInfo[i]);
try { Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
bus.put("DONE");
}
}
public class Consumer implements Runnable {
private Bus bus;
public Consumer(Bus bus) { this.bus = bus; }
public void run() {
Random random = new Random();
for (String message = bus.take();
! message.equals("DONE"); message = bus.take()) {
System.out.format("MESSAGE RECEIVED: %s%n", message);
try { Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
}
}
public class Consumer implements Runnable {
private Bus bus;
public Consumer(Bus bus) { this.bus = bus; }
public void run() {
Random random = new Random();
for (String message = bus.take();
! message.equals("DONE"); message = bus.take()) {
System.out.format("MESSAGE RECEIVED: %s%n", message);
try { Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
}
}
public class ProducerConsumerExample {
public static void main(String[] args) {
Bus bus = new Bus();
(new Thread(new Producer(bus))).start();
(new Thread(new Consumer(bus))).start();
}
}
taouna
شنبه 24 اردیبهشت 1390, 10:37 صبح
سلام
مساله تولید کننده مصرف کننده را میخاستم که یه صف داشته باشه
taouna
شنبه 24 اردیبهشت 1390, 10:46 صبح
دوست عزیز تشکر
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.