0% found this document useful (0 votes)
10 views31 pages

Multi Threading, Frame and EH

Multi threading concept

Uploaded by

Muskan singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views31 pages

Multi Threading, Frame and EH

Multi threading concept

Uploaded by

Muskan singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

MultiThreaded Programming- A process is generally known as threads, java allows you for

creating a program in which more than one process could execute simultaneously.for creating
a multithread programming we can use two techiques-

1. by implementing Runnable interface

2. by extending Thread class

in both the cases you must override run() method which specifies the functionality of
a thread.The general form of run() method will be-

public void run()

body of the method

Some methods of Thread Class-

1. start()- starts the execution of a Thread

2. stop()- stops the execution of a Thread

3. setPriority()- sets the priority of a Thread may be 0,5,10.

4. sleep()- this method pause the execution of thread for a short period of time.It throws the
InterruptedException. it contains time in millisecond as argument.

5. isAlive()- it returns that thread is in working stage or not?

6. join()- stops the execution of next statement until all threads executed.

class thdemo implements Runnable

Thread t;

String name;

int i;
public thdemo(String n)

name=n;

t=new Thread(this);

t.start();

public void run()

for(i=1;i<=5;i++)

System.out.println(name);

try{

Thread.sleep(1000);

}catch(InterruptedException e)

{}

class test

public static void main(String args[])

thdemo t1=new thdemo("Hello");


thdemo t2=new thdemo("Hi");

thdemo t3=new thdemo("Bye");

_____________________________________________________________

using isAlive & join Methods-

class thdemo implements Runnable

Thread t;

String name;

int i;

public thdemo(String n)

name=n;

t=new Thread(this);

t.start();

public void run()

for(i=1;i<=5;i++)

{
System.out.println(name);

try{

Thread.sleep(2000);

}catch(InterruptedException e)

{}

System.out.println(name+" Thread is Exiting.....");

class test

public static void main(String args[])

thdemo t1=new thdemo("Hello");

thdemo t2=new thdemo("Hi");

thdemo t3=new thdemo("Bye");

System.out.println("Hello Thread is Working :"+t1.t.isAlive());

System.out.println("Hi Thread is Working :"+t2.t.isAlive());

System.out.println("Bye Thread is Working :"+t3.t.isAlive());

try{

t1.t.join();

t2.t.join();

t3.t.join();
}catch(InterruptedException e)

{}

System.out.println("Hello Thread is Working :"+t1.t.isAlive());

System.out.println("Hi Thread is Working :"+t2.t.isAlive());

System.out.println("Bye Thread is Working :"+t3.t.isAlive());

System.out.println("main() Thread is Exiting.....");

_________________________________________________________________________

Synchronization in Thread- if more than one process (objects) want to access a single
resourse then the synchronization problem could occur.

class callme

public synchronized void mainfun(String name)

System.out.print("["+name);

try{

Thread.sleep(1000);

}catch(InterruptedException e){}

System.out.println("]");

}
}

class middle implements Runnable

callme ob;

String msg;

public middle(callme o,String n)

ob=o;

msg=n;

new Thread(this).start();

public void run()

ob.mainfun(msg);

class test

public static void main(String args[])

callme obj=new callme();

middle t1=new middle(obj,"Dhananjay");

middle t2=new middle(obj,"Kumar");


middle t3=new middle(obj,"Sharma");

Working with Frame- Frame is used for creating a windows based application.

Constructors

Frame()

Frame(String title)

Methods

setTitle()- sets the title of Frame

setSize(width, height)- sets the size of window

setVisible(boolean value)- sets the visibility of the frame

setLayout()- sets the layout of the frame

Example

import java.awt.*;

class win extends Frame

public win()

setTitle("My First Window");

setSize(400,300);

setVisible(true);

}
class test

public static void main(String args[])

win obj=new win();

____________________________________

Layout Managers- The Layout Managers allows for setting the layout of a window means how
the controls will arrange on the window, it will set by Layout Managers.For setting layout
managers we must use setLayout() method.

Layout Managers Classes are-

1. FlowLayout

2. GridLayout

3. BorderLayout

1. FlowLayout- This is the default layout is the applet.and it arrange the components in
relative manner.

Constructors

FlowLayout()

FlowLayout(int align)

FlowLayout.LEFT

FlowLayout.CENTER
FlowLayout.RIGHT

Example

import java.awt.*;

class win extends Frame

Label l1;

TextField t1;

Button b1;

FlowLayout flow;

public win()

setTitle("My First Window");

flow=new FlowLayout(FlowLayout.LEFT);

setLayout(flow);

l1=new Label("Enter Name ");

t1=new TextField(20);

b1=new Button("Click");

add(l1);

add(t1);

add(b1);

setSize(400,300);

setVisible(true);

}
}

class test

public static void main(String args[])

win obj=new win();

_________________________________________

GridLayout- This class arrange the components in tabular form.

Constructor

GridLayout(int rows,int cols);

import java.awt.*;

class win extends Frame

Label l1,l2,l3;

TextField t1,t2;

Button b1;

GridLayout grid;

public win()

setTitle("My First Window");


grid=new GridLayout(3,2);

setLayout(grid);

l1=new Label("Enter User Name ");

l2=new Label("Enter Password");

l3=new Label("");

t1=new TextField(20);

t2=new TextField(20);

b1=new Button("Login");

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(b1);

setSize(400,150);

setVisible(true);

class test

public static void main(String args[])

win obj=new win();

}
}

____________________________________________

BorderLayout- This Layout Managers Class arrange the elements into specific zone of the
window may be East,West,North,South or Center by default it is Center & this is default
layout of the Frame.

Example

import java.awt.*;

class win extends Frame

Label l1,l2;

public win()

setTitle("My First Window");

setLayout(new BorderLayout());

l1=new Label("Sushant IT College",Label.CENTER);

l1.setBackground(Color.pink);

l2=new Label("Naini");

add(l1,"North");

add(l2,"South");

setSize(400,150);

setVisible(true);

}
class test

public static void main(String args[])

win obj=new win();

__________________________________________________________

import java.awt.*;

class win extends Frame

Label l1,l2;

public win()

setTitle("My First Window");

setLayout(new BorderLayout());

l1=new Label("Sushant IT College",Label.CENTER);

l1.setBackground(Color.pink);

l2=new Label("Naini");

add(l1,"North");

add(l2,"South");

setSize(400,150);

setVisible(true);
}

class test

public static void main(String args[])

win obj=new win();

__________________________________________

Note- for setting your own layout you can use setBounds() method with following syntax-

objectname.setBounds(x,y,width,height)

Example

import java.awt.*;

class win extends Frame

Label l1,l2;

TextField t1,t2;

Button b1;

public win()

setTitle("Login Window");
setLayout(null);

l1=new Label("Enter User Name ");

l2=new Label("Enter Password");

t1=new TextField(20);

t2=new TextField(20);

b1=new Button("Login");

l1.setBounds(10,40,150,20);

t1.setBounds(160,40,120,20);

l2.setBounds(10,70,150,20);

t2.setBounds(160,70,120,20);

b1.setBounds(160,100,120,20);

add(l1);

add(t1);

add(l2);

add(t2);

add(b1);

setSize(400,150);

setVisible(true);

class test

public static void main(String args[])

{
win obj=new win();

Event Handling- Event is an action which indicates happening of any operation.for handling
the event in java we must import java.awt.event package & we must consider on two things-

1. Event Source

2. Event Listener

1. Event Source- It is an object like button, textbox etc on which we perform the event.Each
object support some common events and some special event also.

2. Event Listener Interfaces- java provides a lot of listener interfaces which allows you for
handling the particular event. The listener interfaces provides the scaleton on which we
implement the event.

Steps for handling event-

for handling any event you must follow the following steps-

1. implement the listener interfaces. like

class classname implements EventListenerName

_______

2. Register the event. Like the following-

b1.addListenerType(this);

3. Define all the handlers.


Event Handling Interfaces

ListenerName Handlers Purpose

ActionListener actionPerformed(ActionEvent) When any object click by


mouse

ItemListener itemStateChanged(ItemEvent) When the selected item changes

KeyListener keyPressed(KeyEvent) When any key is pressed, released or typed

keyReleased(KeyEvent)

keyTyped(KeyEvent)

MouseListener mouseClicked(MouseEvent) When mouse related activities


occur

mouseEntered(MouseEvent)

mouseExited(MouseEvent)

mousePressed(MouseEvent)

mouseReleased(MouseEvent)

TextListener textValueChanged(TextEvent) When the value of textbox is


change

WindowListener windowActivated(WindowEvent) When window related activities


occurs

windowClosed(WindowEvent)

windowClosing(WindowEvent)

windowDeactivated(WindowEvent)

windowDeiconified(WindowEvent)

windowIconified(WindowEvent)
windowOpened(WindowEvent)

Using ActionListener-

import java.awt.*;

import java.awt.event.*;

class win extends Frame implements ActionListener

Button b1,b2,b3;

public win()

setTitle("Event Handling Window");

setLayout(new FlowLayout());

b1=new Button("Red");

b2=new Button("Blue");

b3=new Button("Green");

add(b1);

add(b2);

add(b3);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

setSize(400,150);

setVisible(true);

}
public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

setBackground(Color.red);

else if(e.getSource()==b2)

setBackground(Color.blue);

else if(e.getSource()==b3)

setBackground(Color.green);

class test

public static void main(String args[])

win obj=new win();

___________________________________________________________________

Using TextListener-

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class text extends Frame implements TextListener,ActionListener

{
Label l1,l2,l3;

TextField t1,t2,t3;

Button b1;

public text()

setTitle("Welcome");

setLayout(new GridLayout(4,2));

setSize(440,250);

l1=new Label("Basic Salary");

l2=new Label("Ta");

l3=new Label("Da");

t1=new TextField(10);

t2=new TextField(10);

t3=new TextField(10);

b1=new Button("Exit");

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

add(b1);

t1.addTextListener(this);

b1.addActionListener(this);
setVisible(true);

public static void main(String args[])

text e=new text();

public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

System.exit(0);

public void textValueChanged(TextEvent e)

if(e.getSource()==t1)

int bs,ta,da;

bs=Integer.parseInt(t1.getText());

ta=(bs*4)/100;
da=(bs*6)/100;

t2.setText(Integer.toString(ta));

t3.setText(Integer.toString(da));

____________________________________________________________

using ItemListener

import java.awt.*;

import java.awt.event.*;

public class item extends Frame implements ActionListener,ItemListener

Label l1;

Choice c1=new Choice();

Button b1;

public item(String s)

super(s);

setLayout(new FlowLayout());

setSize(240,150);

setBackground(Color.red);
c1.addItem("Red");

c1.addItem("Green");

c1.addItem("Blue");

c1.addItem("Yellow");

l1=new Label("Select Color");

b1=new Button("Exit");

add(l1);

add(c1);

add(b1);

c1.addItemListener(this);

b1.addActionListener(this);

setVisible(true);

public static void main(String args[])

item e=new item("Event Handling");

public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

System.exit(0);

}
}

public void itemStateChanged(ItemEvent e)

if(e.getSource()==c1)

String s;

s=c1.getSelectedItem();

if(s.equals("Red"))

setBackground(Color.red);

else if(s.equals("Blue"))

setBackground(Color.blue);

else if(s.equals("Green"))

setBackground(Color.green);

else if(s.equals("Yellow"))

setBackground(Color.yellow);

________________________________________________________________

using Mouse Listener


import java.awt.*;

import java.awt.event.*;

public class mouse extends Frame implements MouseListener

Button b1;

int w=250,h=150,count=0;

public mouse(String s)

super(s);

setLayout(new FlowLayout());

setSize(w,h);

b1=new Button("Mouse Move");

add(b1);

b1.addMouseListener(this);

setVisible(true);

public static void main(String args[])

mouse e=new mouse("Mouse Handling");

public void mouseExited(MouseEvent e)

if(e.getSource()==b1)
{

setBackground(Color.pink);

public void mousePressed(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{}

public void mouseClicked(MouseEvent e)

count++;

setTitle(Integer.toString(count)+" No of times cliked");

public void mouseEntered(MouseEvent e)

if(e.getSource()==b1)

w+=50;

h+=20;

setSize(w,h);

System.out.println(w+"\t"+h);

setBackground(Color.yellow);

}
}

_____________________________________________________

Key Listener

import java.awt.*;

import java.awt.event.*;

public class key extends Frame implements KeyListener

Button b1;

int x=50,y=200;

public key(String s)

super(s);

setLayout(new FlowLayout());

setSize(240,150);

b1=new Button("KeyPress");

add(b1);

b1.addKeyListener(this);
setVisible(true);

public static void main(String args[])

key e=new key("Key Handling");

public void keyPressed(KeyEvent e)

if(e.getSource()==b1)

setBackground(Color.red);

setSize(300,400);

setLocation(x,y);

x+=10;

y+=20;

b1.setLabel("Leave Me");

public void keyReleased(KeyEvent e)

if(e.getSource()==b1)

setBackground(Color.blue);
b1.setLabel("Press Me");

setSize(240,150);

public void keyTyped(KeyEvent e)

_________________________________________________________

Adapter Classes- The problem of Listener interfaces is that we must define all the handlers
either they are required or not?

We can solve this problem by using Adapter class. The adapter classes are-

1. KeyAdapter

2. MouseAdapter

3. WindowAdapter

Example

import java.awt.*;

import java.awt.event.*;
class win extends Frame

public win()

setTitle("Event Handling Window");

setLayout(new FlowLayout());

setSize(400,150);

addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent e)

System.exit(0);

});

setVisible(true);

class test

public static void main(String args[])

win obj=new win();

}
}

You might also like