Docsity

Java Threads: Creating and Synchronizing Threads in Java, Slides of Computer Science

Toggle sidebar
bg1
1
Java Threads
•Resources
–Course textbook: pp. 162-165
–Java Threads by Scott Oaks & Henry Wong (O’Reilly)
–API docs
•http://download.oracle.com/javase/6/docs/api/
–java.lang.Thread, java.lang.Runnable
–java.lang.Object, java.util.concurrent
–Tutorials
•http://download.oracle.com/javase/tutorial/essential/concurrency/index.html
•http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html
–Introduction to Java Threads
• http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
–Thread safety
•http://en.wikipedia.org/wiki/Thread-safety
•http://www.javaworld.com/jw-08-1998/jw-08-techniques.html
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Java Threads: Creating and Synchronizing Threads in Java and more Slides Computer Science in PDF only on Docsity!

Java Threads

• Resources

  • Course textbook: pp. 162-
  • Java Threads by Scott Oaks & Henry Wong (O’Reilly)
  • API docs
    • http://download.oracle.com/javase/6/docs/api/
      • java.lang.Thread, java.lang.Runnable
      • java.lang.Object, java.util.concurrent
  • Tutorials
    • http://download.oracle.com/javase/tutorial/essential/concurrency/index.html
    • http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html

– Introduction to Java Threads

  • http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html

– Thread safety

  • http://en.wikipedia.org/wiki/Thread-safety
  • http://www.javaworld.com/jw-08-1998/jw-08-techniques.html

Coverage

• Thread class

– run, start methods

– yield, join

– sleep

• Synchronization

– synchronized methods & objects

– wait/notify/notifyAll

– conditions

Example 1

• Create 2 threads from the Main, then start

them

• Threads will be instances of different thread

sub-classes

class MyThreadA extends Thread {

public void run() { // entry point for thread

for (;;) {

System.out.println("hello world1");

class MyThreadB extends Thread {

public void run() { // entry point for thread

for (;;) {

System.out.println("hello world2");

public class Main1 {

public static void main(String [] args) {

MyThreadA t1 = new MyThreadA();

MyThreadB t2 = new MyThreadB();

t1.start();

t2.start();

// main terminates, but in Java the other threads keep running // and hence Java program continues running

Example 2

• Create 2 threads from the Main, then start

them

• Threads will be instances of the same thread

sub-class

• Use argument of constructor of new thread

class to pass text name of thread, e.g.,

“thread1” and “thread2”

– Data member provides different data per thread

(i.e., then name)

– A data member can also be used to share data

class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; }

public void run() { for (;;) { System.out.println(name + ": hello world"); } } }

public class Main2 {

public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); }

}

java.lang.Thread

• public static void yield();

– Method of java.lang.Thread

– Thread gives up CPU for other threads ready to run

class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; }

public void run() { for (;;) { System.out.println(name + ": hello world"); yield(); } } }

public class Main3 {

public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); }

}

More Thread Members: join

• public final void join();

MyThread t1 = new MyThread("thread1");

t1.start();

t1.join();

– Wait until the thread is “not alive”

– Threads that have completed are “not alive” as are

threads that have not yet been started

  • public static void sleep (long millis) throws InterruptedException;
    • Makes the currently running thread sleep (block) for a period of time
    • The thread does not lose ownership of any monitors.
    • InterruptedException - if another thread has interrupted the current thread.

Join Example

Thread State

• public Thread.State getState()

– Returns the state of this thread. This method is

designed for use in monitoring of the system

state, not for synchronization control

http://java.sun.com/javase/6/docs/api/java/lang/Thread.State.htmlDocsity.com

Thread Scheduling in Java

• public final void setPriority(int newPriority);

• public final int getPriority();

• public static final int MAX_PRIORITY

// on my system: 10; Mac OS X 2/21/

• public static final int MIN_PRIORITY

// on my system: 1; Mac OS X 2/21/

• Scheduling

– Priority inherited from parent, but can be changed

– Higher priority threads generally run before lower

priority threads

– For equal priority threads, best to call yield()

intermittently to handle JVM’s with user-level

threading (i.e., no time-slicing)

class SharedData { public int a = 0; public String s = null;

public SharedData() { a = 10; s = "Test"; } }

class MyThread extends Thread { private SharedData m_data = null;

public MyThread(SharedData data) { m_data = data; }

public void run() { for (;;) { m_data.a++; } } }

public class Main5 {

public static void main(String [] args) { SharedData data = new SharedData(); MyThread t1 = new MyThread(data); t1.start();

for (;;) { data.a--; } }

}

If we have multiple threads accessing this

shared data, how do we synchronize access to

ensure it remains in a consistent state?