charThread.java
package thread8;
public class CharThread extends Thread{
private Print print;
public CharThread(Print print) {
super();
this.print = print;
}
@Override
public void run()
{
while(print.getCurrentChar() <= print.getMaxChar())
{
print.printChar();
}
}
}
NumberThread.java
package thread8;
public class NumberThread extends Thread
{
private Print print;
public NumberThread(Print print) {
super();
this.print = print;
}
@Override
public void run()
{
while(print.getCurrentNumber() <= print.getMaxNumber())
{
print.printNumber();
}
}
}
Print.java
package thread8;
public class Print {
private boolean flag;
private char currentChar;
private int currentNumber;
private int charCount;
private int numberCount;
private int startNumber;
private char startChar;
private char maxChar;
private int maxNumber;
public Print(boolean flag,int charCount, int startNumber,char startChar,int numberCount, char maxChar, int maxNumber) {
super();
this.flag = flag;
this.startChar = startChar;
this.startNumber = startNumber;
this.charCount = charCount;
this.numberCount = numberCount;
this.maxChar = maxChar;
this.maxNumber = maxNumber;
this.currentChar = startChar;
this.currentNumber = startNumber;
}
public synchronized void printNumber()
{
try
{
if(flag)
{
for(int index = 1; index <= numberCount && currentNumber <= maxNumber; index++)
{
System.out.print(currentNumber++);
}
flag = false;
notifyAll();
}
else
{
wait();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public synchronized void printChar()
{
try
{
if(!flag)
{
for(int index = 1; index <= charCount && currentChar <= maxChar; index++)
{
System.out.print(currentChar++);
}
flag = true;
notifyAll();
}
else
{
wait();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Test.java
package thread8;
public class Test
{
public static void main(String[] args)
{
Print print = new Print(true, 1, 1, 'A', 2, 'Z', 52);
new CharThread(print).start();
new NumberThread(print).start();
}
}