Lab3 To study and implement multi threading in Java Lab 3 To study and

Lab3 to study and implement multi threading in java

This preview shows page 28 - 35 out of 73 pages.

Lab3 To study and implement multi-threading in Java Lab 3 To study and implement multi-threading in Java Instructions: 1. A thread is an independent unit of execution. 2. In Java, the Runnable interface and Thread class of package java.lang are used for implementation of thread 3. To implement a thread, the desired class must implement the Runnable interface and provide therun() method. public class MyThread implements Runnable { public void run() { //implementation of thread } } 4. The Thread class can then be used to start a thread as follows: public class TestThread { public static void main( String[] args ) { MyThread m = new MyThread(); Thread t = new Thread(m); m.start(); } } LAB TASKS: 1. Write a class that implements Runnable. Define a constructor that takes the name of the thread as argument. The thread upon execution will print the name of the thread in a while loop. Define and run 5 thread objects. What output do you see? CODE: package os_lab_4; public class OS_LAB_4 { public static class MyThread implements Runnable { public String name; 28
Image of page 28
Lab3 To study and implement multi-threading in Java public MyThread(String name) { // call to constructor of the Thread class. this.name = name; } @Override public void run() { //implementation of thread } } public static void main(String[] args) { for(int i=1; i<=5; i++) { MyThread t1 = new MyThread("Zohair" + i); System.out.println(t1.name); Thread t = new Thread(t1); t.start(); } } } OUTPUT: 29
Image of page 29
Lab3 To study and implement multi-threading in Java 2. In task 1, modify the run method to randomly sleep the thread for few milliseconds. Observe the output. CODE: package os_lab_4; public class OS_LAB_4 { public static class MyThread implements Runnable { public String name; public MyThread(String name) { 30
Image of page 30
Lab3 To study and implement multi-threading in Java // call to constructor of the Thread class. this.name = name; } @Override public void run() { //implementation of thread System.out.println("Thread is running.. \n"); while(true) { System.out.println(name); try { Thread.sleep(1000); } catch(Exception e) {} } } } public static void main(String[] args) { for(int i=1; i<=5; i++) { MyThread t1 = new MyThread("Zohair " + i); 31
Image of page 31
Lab3 To study and implement multi-threading in Java System.out.println(t1.name); Thread t = new Thread(t1); // getting the above created threads names. t.start(); } } } OUTPUT: 3. Create a multi-threaded client server application in Java. CODE: MyThread.java: package myrunable; public class MyThread implements Runnable{ private String name; 32
Image of page 32
Lab3 To study and implement multi-threading in Java @Override public void run() { while(true){ System.out.println(name); try{ Thread.sleep(1000); } catch(Exception ex){} } } public MyThread(String name) { this.name = name; } public static void main(String[] args) { try { for(int i=1;i<=5;i++) { MyThread m = new MyThread("Test "+i); Thread t = new Thread(m); t.start(); } 33
Image of page 33
Lab3 To study and implement multi-threading in Java } catch(Exception ex) { System.out.println(""); } } } MyRunnable.java: package myrunable; import java.io.*; import java.net.*; import java.nio.CharBuffer; public class Myrunable implements Runnable{ private Socket s; public Myrunable(Socket s) { this.s = s; } @Override public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(dis.readUTF()); s.close(); } 34
Image of page 34
Image of page 35

You've reached the end of your free preview.

Want to read all 73 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors