Re: Newbie to java
Jack schrieb:
import java.lang.Thread;
public class HelloWorld extends Thread {
public void run()
{
System.out.println("Hello World");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t = new HelloWorld();
CharacterBuffer cb;
^^^^^^^^^^^^^^^^^^^^
You have to assign a value to cb, like "new
CharacterBuffer()"
cb.addChar('c'); <<<<<<<<<<<<<<< error
t.start();
}
}
=============================
public class CharacterBuffer
{
private byte[] data_ = null;
private int len_ = 0;
synchronized public void addChar(byte c)
{
if (data_ == null || len_ == data_.length)
{
byte[] newData = new byte[len_+128];
if (data_ != null)
System.arraycopy(data_, 0, newData,0, len_);
data_ = newData;
}
data_[len_++] = c;
}
synchronized public void writeBuffer()
{
System.out.write (data_, 0, len_);
System.out.flush();
len_ = 0;
}
}
======================================
besides I'd like to make the thread print "abc123", how do I make it
work like that?
If you want the thread to print it, you should use the CharacterBuffer
in its run() method.
Hint:
To print "123abc", look at the CharacterBuffer class and find out was it
does. It has two methods that cover your demands.
Thanks
Jack
Tobi