Java Programs: Threads, Sync, Frame Windows, Drawing Shapes with Interfaces and Libraries, Exercises of Java Programming

Five java programs that demonstrate various concepts such as creating threads using runnable interface, synchronization in threads, creating frame windows in an applet, and drawing graphical shapes. The first program creates and starts three threads using the mythread class that implements runnable interface. The second program illustrates synchronization by having two threads print a counter. The third program creates a frame window within an applet. The fourth program creates a menu and submenu within an applet. The fifth program draws various shapes using the graphics class.

Typology: Exercises

2018/2019

Uploaded on 03/26/2019

Tomny
Tomny 🇿🇲

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q.1 Write a Java program to create a thread using Runnable interface
package mythread;
class MyThread implements Runnable {
String name;
Thread t;
MyThread (String thread){
name = "threadname";
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThread {
public static void main(String args[]) {
new MyThread("One");
new MyThread("Two");
new MyThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
Q.2 Write a Java program to illustrate synchronization concept in Threads
pf3
pf4
pf5

Partial preview of the text

Download Java Programs: Threads, Sync, Frame Windows, Drawing Shapes with Interfaces and Libraries and more Exercises Java Programming in PDF only on Docsity!

Q.1 Write a Java program to create a thread using Runnable interface

package mythread; class MyThread implements Runnable { String name; Thread t; MyThread (String thread){ name = "threadname"; t = new Thread( this , name); System. out .println("New thread: " + t); t.start(); } public void run() { try { for ( int i = 5; i > 0; i--) { System. out .println(name + ": " + i); Thread. sleep (1000); } } catch (InterruptedException e) { System. out .println(name + "Interrupted"); } System. out .println(name + " exiting."); } } class MultiThread { public static void main(String args[]) { new MyThread("One"); new MyThread("Two"); new MyThread("Three"); try { Thread. sleep (10000); } catch (InterruptedException e) { System. out .println("Main thread Interrupted"); } System. out .println("Main thread exiting."); } }

Q.2 Write a Java program to illustrate synchronization concept in Threads

class PrintDemo { public void printCount() { try { for ( int i = 5; i > 0; i--) { System. out .println("Counter --- " + i ); } } catch (Exception e) { System. out .println("Thread interrupted."); } } }

class ThreadDemo extends Thread { private Thread t; private String threadName; PrintDemo PD;

ThreadDemo( String name, PrintDemo pd) { threadName = name; PD = pd; }

public void run() { synchronized (PD) { PD.printCount(); } System. out .println("Thread " + threadName + " exiting."); }

public void start () { System. out .println("Starting " + threadName ); if (t == null ) { t = new Thread ( this , threadName); t.start (); } } }

public class SyncRunner {

public static void main(String args[]) { PrintDemo PD = new PrintDemo();

ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD ); ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );

T1.start(); T2.start();

// wait for threads to end try { T1.join(); T2.join();

//Create a child frame window from within an applet. import java.awt.; import java.awt.event.; import java.applet.; /

class SampleFrame extends Frame { SampleFrame(String title) { super (title); MyWindowAdapter adapter = new MyWindowAdapter( this );

addWindowListener(adapter); } public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); } } class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this .sampleFrame = sampleFrame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible( false ); }} public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible( true ); } public void start() { f.setVisible( true ); } public void stop() { f.setVisible( false ); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); } }

Q.4 Write an applet program to illustrate the Menu and Submenu controls

package appMenu;

import java.awt.*; class AWTMenu extends Frame { MenuBar mbar; Menu menu,submenu, chips; MenuItem m1,m2,m3,m4,m5,m9,m10, m11;

public AWTMenu() {

// Set frame properties setTitle("JOSEE AWT "); // Set the title setSize(400,400); // Set size to the frame setLayout( new FlowLayout()); // Set the layout setBackground(Color. magenta ); setVisible( true ); // Make the frame visible setLocationRelativeTo( null ); // Center the frame

// Create the menu bar mbar= new MenuBar();

// Create the menu menu = new Menu("Programmes"); chips= new Menu("Lecturer");

// Create the submenu submenu= new Menu("MSIt");

// Create MenuItems m1= new MenuItem("mobile programming"); m2= new MenuItem("Java programming"); m3= new MenuItem("LAN MAN"); m4= new MenuItem("computer architecture"); m5= new MenuItem(" Wed Development ");

m9= new MenuItem(" Mr. Lameck Nsama"); m10= new MenuItem("Mr. Thinakaran Anansingh"); m11 = new MenuItem("Mr. Chuunga "); // Attach menu items to menu menu.add(m1); menu.add(m2); menu.add(m3); chips.add(m9); chips.add(m10); chips.add(m11); // Attach menu items to submenu submenu.add(m4); submenu.add(m5);

// Attach submenu to menu menu.add(submenu);

// Attach menu to menu bar mbar.add(menu);

public class fillColor extends Applet{

public void paint(Graphics g){

g.drawLine(30,300,200,10); g.drawOval(250,50,100,100); g.drawRect(400,50,200,100);