• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Why dont I get anything out in the console window?

What am I missing? I dont get anything out in the console window. :-/
import java.awt.*;
 
public class MainP3 {
    public static void main(String[] args) throws MessageException, InterruptedException {
	
        try {
            Message[] messages = {new Message("Nu är det november och sedan är det december", Color.yellow, Color.black, new Font("SansSerif", Font.PLAIN, 24)),
                                  new Message("En adventskalender med godis piggar alltid upp", Color.white, Color.red, new Font("Serif", Font.BOLD, 16)),
                                  new Message("Jultomte söker jobb\nring 0722-11223344", Color.red, Color.black, new Font("Monospaced", Font.ITALIC, 20))};
            MessageBuffer buffer = new MessageBuffer();
            MessageViewer viewer = new SimpleViewer();
            Editor editor = new Editor(viewer, buffer, 10000);
            Producer producer = new MessageFromArray(messages, buffer, 1000);
            editor.startEditor();
        } catch (MessageException e) {}    
}
}
 
---------------------------------------------------------------------------
 
public class MessageBuffer {
	private Message message;
	
	public void put(Message message) {
		this.message = message;		
	}
 
	public Message get() {		
		return message;
	}
}
 
----------------------------------------------------------------------------------------
import java.awt.Color;
 
public interface MessageViewer {	
    public void setMessage(Message message);    
    public void noMessage();
}
----------------------------------------------------------------------------------------------------
import java.awt.*;
 
public class SimpleViewer implements MessageViewer {    
    
    // Prints the text-attribute in a Message-object
    public void setMessage(Message message) {
        System.out.println("----------------------------------------------");
        System.out.println(message.getText());
    }
    // No action in DOS-window
    public void noMessage() {
    }        
}
 
-----------------------------------------------------------------------------------
public class Editor {
 
	private MessageViewer viewer;
	private MessageBuffer buffer;
	private int i;	
 
	public Editor(MessageViewer viewer, MessageBuffer buffer, int i) {
		this.viewer = viewer;
		this.buffer = buffer;
		this.i = i;
	}
 
	public void startEditor() {			
	}
}
---------------------------------------------------------------------------------
 
 
public abstract class Producer implements Runnable {
    private Thread thread;
    private MessageBuffer buffer;
    private long delay;
    
    public Producer(MessageBuffer buffer, long delay) {
        this.buffer = buffer;
        this.delay = delay;
    }
    
    protected void produce() {
        thread = new Thread(this);
        thread.start();
    }
    
    protected abstract Message getNext();
    
    public void run() {
        Message message = getNext();
        while( !thread.interrupted() && (message != null) ) {
            buffer.put(message);
            try {
                Thread.sleep(delay);
            }
            catch( InterruptedException e) {
                break;
            }
            message = getNext();
        }
            System.out.println(Thread.currentThread().getName() + " avslutas");
    }
}

Open in new window

0
Mickeys
Asked:
Mickeys
  • 19
  • 18
  • 6
1 Solution
 
CEHJCommented:
run doesn't seem to be doing any printing to me ...
0
 
MickeysAuthor Commented:
Here I print:

public void setMessage(Message message) {
        System.out.println("----------------------------------------------");
        System.out.println(message.getText());
    }

where is it you mean?
0
 
objectsCommented:
startEditor() does nothing.
and you never call produce() so the thread never gets started

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
you never call setMessage()

0
 
MickeysAuthor Commented:
CEHJ: Thx for the help yesterday. I got everything to work fine there. I just have to do the connection to the database as well. :-/

Anyway......about this problem. What exactly is it I should do in startEditor?  I only got the main so it is kind of hard to guess what I should do. *hehe*

am I also supposted to put producer.run(); in main?
0
 
CEHJCommented:
You probably need an instance variable of type Producer in Editor then start it in Editor.startEditor
0
 
CEHJCommented:
... and starting it would involve calling Producer.produce of course
0
 
objectsCommented:
start with starting your Producer (by calling produce())
Not sure what you want the Editor to do, seems pretty pointless class at the moment. Whay do you have a startEditor() method if you don't know what its for :)


0
 
MickeysAuthor Commented:
well I tried to put a producer berfore in the editor but I get this error
Cannot instantiate the type Producer
when I type:
Producer prod = new Producer(buffer, i);
Why? and how do I get around this problem.

objects: I got a startEditor because there is one in the main. I only got the main code and I have to write the rest. :-/ So it is not that easy.....atleast I dont think so.
0
 
CEHJCommented:
You need to create a subclass of Producer - it's abstract
0
 
objectsCommented:
> Why? and how do I get around this problem.

you are already creating an instance of Producer

0
 
MickeysAuthor Commented:
Ok. That I have already created. See code below.

So in editor I have added
Message[] messages;

and in the constructor included so it brings in the messages

then in startEditor I have
Producer producer = new MessageFromArray(messages, buffer, 1000);
            producer.produce();

1. I dont think I am supposted to add a new string in the constructor. Is there away around this?
2. It only writes out my threads and´not my messages.
3. Am I that stupied?
public class MessageFromArray extends Producer {
    private Message[] messages;
    private int index = 0;
    
    public MessageFromArray(Message[] messages, MessageBuffer buffer, long delay) {
        super(buffer, delay);
        this.messages = messages.clone();
        super.produce();
    }
    
    public Message getNext() {
        Message res = null;
        if( index < messages.length ) {
            res = messages[index];
            index++;
        }
        return res;
    }
}

Open in new window

0
 
objectsCommented:
>             producer.produce();

you don't need that, its called in the constructor

0
 
MickeysAuthor Commented:
ok.

but what am I missing in my code?

if I run this:

MessageViewer viewer = new SimpleViewer();
          Message m1 = new Message("Nu är det november och sedan är det december",
          Color.yellow, Color.black,
          new Font("SansSerif", Font.PLAIN, 24));
          Message m2 = new Message("En adventskalender med godis piggar alltid upp",
          Color.white, Color.red,
          new Font("Serif", Font.BOLD, 16));
          Message m3 = new Message("Jultomte söker jobb\nring 0722-11223344",
          Color.red, Color.black,
          new Font("Monospaced", Font.ITALIC, 20));
          viewer.noMessage();
          Thread.sleep(5000);
          viewer.setMessage(m1);
          Thread.sleep (5000);
          viewer.setMessage(m2);
          Thread.sleep (5000);
          viewer.setMessage(m3);
          Thread.sleep (5000);
          viewer.noMessage();

then it works
0
 
objectsCommented:
you need to wire in your Editor, such that it is doing similar. Perhaps in a separate thread something like this:

public class Editor {
 
        private MessageViewer viewer;
        private MessageBuffer buffer;
        private int i;  
 
        public Editor(MessageViewer viewer, MessageBuffer buffer, int i) {
                this.viewer = viewer;
                this.buffer = buffer;
                this.i = i;
        }
 
        public void startEditor() {                    
           new Thread(this).start();
        }

        public void run() {
             while (true) {
                   viewer.setMessage(buffer.get());
                   Thread.sleep(1000);
        }
}


0
 
CEHJCommented:
In run(), in addition to

>>message = getNext();

you need to do

System.out.println(message.getText());
0
 
CEHJCommented:
You should remove the debug printing in setMessage
0
 
objectsCommented:
public class Editor implements Runnable {
 
        private MessageViewer viewer;
        private MessageBuffer buffer;
        private int i;  
 
        public Editor(MessageViewer viewer, MessageBuffer buffer, int i) {
                this.viewer = viewer;
                this.buffer = buffer;
                this.i = i;
        }
 
        public void startEditor() {                    
           new Thread(this).start();
        }

        public void run() {
             while (true) {
                   viewer.setMessage(buffer.get());
                   Thread.sleep(1000);
             }
        }
}

0
 
MickeysAuthor Commented:
NOW things starts to happen. :-)

Last thing:
The output gets like this:
Nu är det november och sedan är det december
Thread-1 avslutas
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------


and then it just keps on going with the last one and never ends. Is there a way that it only goes as long as the private int i in Editor is?
0
 
objectsCommented:
>              while (true) {

replace that with:

             while (i-- >0) {

0
 
MickeysAuthor Commented:
well that makes it ends.

What I thought this program was doing was to create like two threads and then run them....but the output gets like below. That means it starts one thread.....and then i just keeps going on until i-- >0.  Shouldnt the program just stop after it says thread 1-avslutad.


----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-1 avslutas
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
0
 
objectsCommented:
it does create 2 threads, one for the editor and the other for the producer

0
 
MickeysAuthor Commented:
It runns but I dont think that is what the output should be. I must be that the output should be:

----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-1 avslutas
----------------------------------------------

and if there is another thread

----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-2avslutas
----------------------------------------------

or am I wrong?
0
 
objectsCommented:
one thread (producer) is taking messages from the buffer
the other thread (editor) is taking message from the buffer and passing it to the viewer
 
0
 
MickeysAuthor Commented:
ok. So I guess it should only be producer that should be running. But that was what I was trying before but I didnt get it to work. Is there a way to do that?
0
 
objectsCommented:
thought u said you needed two threads, seems a standard way to do it.
What exactly is the problem currently?


0
 
MickeysAuthor Commented:
Well when we started I didnt get anything at all out on my output.

after you showed me how editor class should be done I get two threads printing all the stuff.

one printing this:
----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-1 avslutas
----------------------------------------------

and one pritning alot of:
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
----------------------------------------------

What I see it should only be printing this
----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-1 avslutas
----------------------------------------------

and then end. Incase I start another thread up there will be two of those printing.

PROBLEM: ....if I remove the editor code I wont get any stuff at all. And if I keep it I get two diffrent threads printing stuff.
0
 
MickeysAuthor Commented:
Subklass to Producer  ----MO--->MessageBuffer----MO---->  Editor -----MO---->  Implementing of MessageViewer
0
 
objectsCommented:
should only be your viewing printing to the console
have you changed the code?

0
 
MickeysAuthor Commented:
I havent changed anything except for what you guys have trying to teach me. Well I have tried some stuff but when it doesnt work I change back.

So could you send me to the correct class and row so I can see the code that you say is the problem? Right now everything is just spinning around and I dont understand anything.
0
 
objectsCommented:
you should only be printing out the message in the view er where you originally had it. You shouldn't be printing the message anywhere else in your code.

0
 
MickeysAuthor Commented:
but the only class I have changed is Editor class.

When I had it like before nothing happened.

When I changed it to the way you showed me it prints twice.
0
 
objectsCommented:
make sure you aren't printing in the editor class
post it here if you think it looks ok

0
 
MickeysAuthor Commented:
aint printing in there
public class Editor implements Runnable {
	 
    private MessageViewer viewer;
    private MessageBuffer buffer;
    private int i;  
 
    public Editor(MessageViewer viewer, MessageBuffer buffer, int i) {
            this.viewer = viewer;
            this.buffer = buffer;
            this.i = i;
    }
 
    public void startEditor() {                     
       new Thread(this).start();
    }
 
    public void run() {
    	
         while (i-->0) {
               viewer.setMessage(buffer.get());
              
               try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
         }
    }
}

Open in new window

0
 
objectsCommented:
can you post the others as well again
0
 
MickeysAuthor Commented:
Here u go.

Output should be:
----------------------------------------------
Nu är det november och sedan är det december
----------------------------------------------
En adventskalender med godis piggar alltid upp
----------------------------------------------
Jultomte söker jobb
ring 0722-11223344
Thread-1 avslutas
----------------------------------------------

public class MainP3 {
    public static void main(String[] args) throws MessageException, InterruptedException {        try {
            Message[] messages = {new Message("Nu är det november och sedan är det december", Color.yellow, Color.black, new Font("SansSerif", Font.PLAIN, 24)),
                                  new Message("En adventskalender med godis piggar alltid upp", Color.white, Color.red, new Font("Serif", Font.BOLD, 16)),
                                  new Message("Jultomte söker jobb\nring 0722-11223344", Color.red, Color.black, new Font("Monospaced", Font.ITALIC, 20))};
            MessageBuffer buffer = new MessageBuffer();
            MessageViewer viewer = new SimpleViewer();
            Editor editor = new Editor(viewer, buffer, 10);
            Producer producer = new MessageFromArray(messages, buffer, 1000);
            editor.startEditor();            
        } catch (MessageException e) {}    
}
}
 
---------------------------------------------
 
public class MessageBuffer {
 
	private Message message;
	
 
	public void put(Message message) {
		this.message = message;
		
	}
 
	public Message get() {
		
		return message;
	}
}
 
-----------------------------------------------------------
public interface MessageViewer {
 
	
    public void setMessage(Message message);
    
    public void noMessage();
}
------------------------------------------
public class SimpleViewer implements MessageViewer {    
    
    // Prints the text-attribute in a Message-object
    public void setMessage(Message message) {
        System.out.println("----------------------------------------------");
        System.out.println(message.getText());
    }
    // No action in DOS-window
    public void noMessage() {
    }    
    
}
 
---------------------------------------
public class Editor implements Runnable {
	 
    private MessageViewer viewer;
    private MessageBuffer buffer;
    private int i;  
 
    public Editor(MessageViewer viewer, MessageBuffer buffer, int i) {
            this.viewer = viewer;
            this.buffer = buffer;
            this.i = i;
    }
 
    public void startEditor() {                     
       new Thread(this).start();
    }
 
    public void run() {
    	
         while (i-->0) {
               viewer.setMessage(buffer.get());
              
               try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
         }
    }
}
-----------------------------------------------------------
 
public abstract class Producer implements Runnable {
    private Thread thread;
    private MessageBuffer buffer;
    private long delay;
    
    public Producer(MessageBuffer buffer, long delay) {
        this.buffer = buffer;
        this.delay = delay;
    }
    
    protected void produce() {
        thread = new Thread(this);
        thread.start();
    }
    
    protected abstract Message getNext();
    
    public void run() {
        Message message = getNext();
        while( !thread.interrupted() && (message != null) ) {
            buffer.put(message);
            try {
                Thread.sleep(delay);
            }
            catch( InterruptedException e) {
                break;
            }
            message = getNext();            
        }
            System.out.println(Thread.currentThread().getName() + " avslutas");
    }
}
 
--------------------------------------------------------
 
public class MessageFromArray extends Producer {
    private Message[] messages;
    private int index = 0;
    
    public MessageFromArray(Message[] messages, MessageBuffer buffer, long delay) {
        super(buffer, delay);
        this.messages = messages.clone();
        super.produce();
    }
    
    public Message getNext() {
        Message res = null;
        if( index < messages.length ) {
            res = messages[index];
            index++;
        }
        return res;
    }
}

Open in new window

0
 
objectsCommented:
are there are any of the classes you are not meant to change?

0
 
MickeysAuthor Commented:
well it says I should do:
Editor
MessageBuffer

the rest of the classes have I writen in other assagnments so I think they should be as they are since we should reuse them now.

but if there is a must in changing some class I dont think that is a problem. As long as main is as it is.
0
 
objectsCommented:
try this MessageBuffer:

public class MessageBuffer {
    private Message message = null;

    public synchronized Message get() {
        while (message==null) {
            try {  wait();  } catch (InterruptedException e) {}
        }
        message = null;
        notifyAll();
        return message;
    }

    public synchronized void put(Message message) {
        while (!message!=null) {
            try {  wait();  } catch (InterruptedException e) {}
        this.message = message;
        notifyAll();
    }
}

0
 
MickeysAuthor Commented:
if I exchange that to what you have and leave editor as you did it before with runnable all it writes is:
Thread-1 avslutas
0
 
MickeysAuthor Commented:
if I debug from main.

Messages gets all three strings.
buffer -> message is null
viewer  gets created
0
 
objectsCommented:
woops, try this :)

public class MessageBuffer {
    private Message message = null;
    private boolean empty = true;

    public synchronized Message get() {
        while (empty) {
            try {  wait();  } catch (InterruptedException e) {}
        }
        empty = true;
        notifyAll();
        return message;
    }

    public synchronized void put(Message message) {
        while (!empty) {
            try {  wait();  } catch (InterruptedException e) {}
        }
        empty = false;
        this.message = message;
        notifyAll();
    }
}

0
 
MickeysAuthor Commented:
YESSSSSSSSSSSSSSSSSSSSSSSSSSSS it works. No I am gonna sit down and debug it so I  really understand what happens.

THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 19
  • 18
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now