Wednesday, 21 September 2011

NEXT LAB PROGRAMS:

PRODUCER CONSUMER USING THREAD QUEUE CLASS:
class Q
{
    int n;
    boolean valueSet=false;
    synchronized int get()
    {
        if(!valueSet)
            try
            {
                wait();
            }catch(InterruptedException e)
            {
                System.out.println("InterruptedException caught");
            }
        System.out.println("Consumer consumes:Got: "+n);
        valueSet=false;
        notify();
        return n;
    }
    synchronized void put(int n)
    {
        if(valueSet)
            try
            {
                wait();
            }catch(InterruptedException e)
            {
                System.out.println("InterruptedExeption caught");
            }
        this.n=n;
        valueSet=true;
        System.out.println("producer input -Put: "+n);
        notify();
    }
}
class Producer implements Runnable
{
    Q q;
    Producer(Q q)
    {
        this.q=q;
        new Thread(this,"Producer").start();
    }
    public void run()
    {
        int i=0;
        while(i<20)
        {
            q.put(i++);
        }
    }
}
class Consumer implements Runnable
{
    Q q;
    Consumer(Q q)
    {
        this.q=q;
        new Thread(this,"Consumer").start();
    }
    public void run()
    {
        while(true)
        {
            q.get();
        }
    }
}
class PCQue
{
    public static void main(String args[])
    {
        Q q=new Q();
        new Producer(q);
        new Consumer(q);
        System.out.println("Press Control-C to stop!");
    }
}


CLIENT SERVER COMMUNICATION:
EchoClient.java:
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class EchoClient extends JFrame implements ActionListener
{
    JTextField tf=new JTextField(15);
    JTextField tf2=new JTextField(15);
    BufferedReader in;
    PrintWriter out;
    JButton b;
    public EchoClient()
    {
        try
        {
            JFrame f=new JFrame();
            f.setSize(100,100);
            Socket socket=new Socket("localhost",8008);
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            f.setTitle("client"+socket);
            tf.setText(" ");
            tf2.setText(" ");
            JPanel p=new JPanel();
            JLabel l=new JLabel("enter the text to sent to the server");
            JLabel l2=new JLabel("Text echoed from server in uppercase");
            b=new JButton("send");
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.add(l);
            p.add(tf);
            p.add(l2);
            p.add(tf2);
            p.add(b);
            f.add(p);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(this);
            out.flush();
            while(true)
            {
                String str=in.readLine();
                tf2.setText(str);
            }
        }catch(Exception e)
        {}
    }
public void actionPerformed(ActionEvent event)
{
    String str1=tf.getText();
    System.out.println("sending "+str1);
    out.println(str1.toString());
    out.flush();
    tf.setText(" ");
}
public static void main(String args[])
{
    EchoClient f=new EchoClient();
}
}

EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[])throws Exception
{
    try
    {
        ServerSocket s=new ServerSocket(8008);
        while(true)
        {
            Socket incoming=s.accept();
            new ClientHandler(incoming).start();
        }
    }catch(Exception e)
    {}
    }
}
class ClientHandler extends Thread
{
    Socket incoming;
    ClientHandler(Socket incoming)
    {
        this.incoming=incoming;
    }
    public void run()
    {
        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter out=new PrintWriter(new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())));
            out.flush();
            while(true)
            {
                String str=in.readLine();
                System.out.println("receiving from "+incoming+" "+str);
                out.println(str.toUpperCase());
                out.flush();
                if(str.trim().endsWith("BYE"))
                    break;
            }
            incoming.close();
        }catch(Exception e)
        {}
    }
}

TEMP LIST JAVA:
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class TempList
{
final String colors[]={"red "," blue" ,"green ","grey "};
final String numbers[]={"One "," Two","Three "};

TempList()
{
List<String> list1=new LinkedList();
List<String> list2=new LinkedList();
List<String> list3=new LinkedList();
for(String s:colors)
{
    list1.add(s);
}
System.out.println("-------List1------");
printList(list1);
for(String s:numbers)
{
    list2.add(s);
}
System.out.println("-------List2------");
printList(list2);
list3.addAll(list2);
System.out.println("-------List3------");
printList(list3);
list3.addAll(list1);
printList(list3);
list3.retainAll(list2);
System.out.println("-------After reatining--------");
printList(list3);
addItem(list3," four ");
System.out.println("-------After adding--------");
printList(list3);
addItem(list3," five ",4);
System.out.println("-------After adding--------");
printList(list3);
removeItem(list3,1);
System.out.println("-------After removing--------");
printList(list3);
removeItem(list3,2,4);
System.out.println("-------After removing--------");
printList(list3);
toUpperCaseString(list3);
printList(list3);

}
void addItem(List<String> list,String str)
{
list.add(str);
}
void addItem(List<String> list,String str,int index)
{
list.add(index,str);
}
void removeItem(List<String> list,int index)
{
list.remove(index);
}
void toUpperCaseString(List<String> list)
{
ListIterator<String> iterator=list.listIterator();
while(iterator.hasNext())
{
String str=iterator.next();
iterator.set(str.toUpperCase());
}
}
void removeItem(List<String> list,int s,int e)
{
list.subList(s,e).clear();
}

void printList(List<String> list)
{


for(String s:list)
{
    System.out.print(s);
}
System.out.println();
}
public static void main(String args[])
{
    new TempList();
}
}






No comments:

Post a Comment