Skip to content
Snippets Groups Projects
Commit 2547f25e authored by Eckl, Máté's avatar Eckl, Máté
Browse files

5. labor kötelező feladatok kész

parent 8598926f
No related branches found
No related tags found
No related merge requests found
......@@ -6,3 +6,4 @@ build.xml
dist/
manifest.mf
nbproject/
n-ary tree/
public class Application {
public static void main(String args[]) throws InterruptedException
{
Fifo storage = new Fifo();
Producer prod = new Producer(storage, "producer", 250);
Consumer con = new Consumer(storage, "consumer", 500);
Thread t1 = new Thread(prod);
Thread t2 = new Thread(con);
t1.start();
t2.start();
}
}
\ No newline at end of file
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Consumer implements Runnable{
Fifo storage;
String message;
int sleepTime;
public Consumer(Fifo f, String s, int n)
{
storage = f;
message = s;
sleepTime = n;
}
@Override
public void run()
{
while(true)
{
try {
System.out.println("consumed " + message + " " +
storage.get() + " " +
System.currentTimeMillis());
sleep(sleepTime);
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
import java.util.ArrayList;
public class Fifo{
protected ArrayList<String> s;
{
s = new ArrayList<>(10); //csak azért, hogy használjak már inicializáló blokkot is
}
public synchronized void put(String e) throws InterruptedException
{
if(s.size()>=10)
{
this.wait();
}
this.notifyAll();
s.add(e);
System.out.println("produced" + " " + e + " " +
System.currentTimeMillis());
}
synchronized String get() throws InterruptedException
{
if(s.size()<=0)
{
this.wait();
}
this.notifyAll();
String ret=s.get(0);
s.remove(ret);
return ret;
}
}
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Producer implements Runnable{
private String message;
private Fifo storage;
private int sleepTime;
public Producer(Fifo f, String s, int n)
{
message = s;
storage = f;
sleepTime = n;
}
public void go() throws InterruptedException
{
int i=0;
while(true)
{
storage.put(message + " " + i++);
try {
sleep(sleepTime);
}
catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void run()
{
try {
this.go();
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment