From Documentation
Jump to: navigation, search
(Introduction)
Line 1: Line 1:
 
=Introduction=
 
=Introduction=
This first half of this article is a summary of Oracle's java concurrency pages [http://docs.oracle.com/javase/tutorial/essential/concurrency/], while the second half will serve as a repository for Java Thread code examples.  Java was designed from the bottom up to support threaded programming, and shares a number of concepts with the C language threaded paradigm PThreads.  This article assumes a familiarity with the concept of threads, as well as a functional understanding of the Java programming language.  Before continueing please review the specifics of working with [[OPENJDK]] on Sharcnet.  All of the examples provided have been tested on Sharcnet, specifically on the Orca development nodes.
+
The Java threads instructions and example will be laid out over two articles. This first article is a summary of Oracle's java concurrency pages [http://docs.oracle.com/javase/tutorial/essential/concurrency/], while the second article will serve as a repository for Java Thread code examples.  Java was designed from the bottom up to support threaded programming, and shares a number of concepts with the C language threaded paradigm PThreads.  This article assumes a familiarity with the concept of threads, as well as a functional understanding of the Java programming language.  Before continueing please review the specifics of working with [[OPENJDK]] on Sharcnet.  All of the examples provided have been tested on Sharcnet, specifically on the Orca development nodes.
  
 
I have started this article as opposed to just forwarding to the Oracle page so that I will be able to post answers to common questions here, or linked to here.  On that note, feel free to update this page with your experiences/tips, and forward any bugs, questions, or problems to [mailto:edward@sharcnet.ca Ed Armstrong (edward@sharcnet.ca)], or submit a ticket in the SHARCNET  [https://www.sharcnet.ca/my/problems/submit problem tracking system].
 
I have started this article as opposed to just forwarding to the Oracle page so that I will be able to post answers to common questions here, or linked to here.  On that note, feel free to update this page with your experiences/tips, and forward any bugs, questions, or problems to [mailto:edward@sharcnet.ca Ed Armstrong (edward@sharcnet.ca)], or submit a ticket in the SHARCNET  [https://www.sharcnet.ca/my/problems/submit problem tracking system].
  
=Thread Creation and Execution=
+
The following is the Oracle license agreement, which applies to the code samples in this article.
 +
<pre>
 +
Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 +
Redistribution and use in source and binary forms, with or without
 +
modification, are permitted provided that the following conditions
 +
are met:
 +
  - Redistributions of source code must retain the above copyright
 +
    notice, this list of conditions and the following disclaimer.
 +
  - Redistributions in binary form must reproduce the above copyright
 +
    notice, this list of conditions and the following disclaimer in the
 +
    documentation and/or other materials provided with the distribution.
 +
  - Neither the name of Oracle or the names of its
 +
    contributors may be used to endorse or promote products derived
 +
    from this software without specific prior written permission.
 +
 
 +
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 +
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 +
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 +
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 +
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 +
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 +
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 +
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 +
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 +
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 +
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 +
</pre>
 +
 
 +
=Thread Creation, Execution, and Control=
 
There are two ways to create and start a Java thread. The first involves implementing the Runnable[http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html] interface and passing an instance to the Thread class constructor.  The second involves extending the Thread[http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html] class it's self, and overriding the provided, yet empty, run method.
 
There are two ways to create and start a Java thread. The first involves implementing the Runnable[http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html] interface and passing an instance to the Thread class constructor.  The second involves extending the Thread[http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html] class it's self, and overriding the provided, yet empty, run method.
  
Line 30: Line 58:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
==Thread.sleep()==

Revision as of 14:13, 11 August 2014

Introduction

The Java threads instructions and example will be laid out over two articles. This first article is a summary of Oracle's java concurrency pages [1], while the second article will serve as a repository for Java Thread code examples. Java was designed from the bottom up to support threaded programming, and shares a number of concepts with the C language threaded paradigm PThreads. This article assumes a familiarity with the concept of threads, as well as a functional understanding of the Java programming language. Before continueing please review the specifics of working with OPENJDK on Sharcnet. All of the examples provided have been tested on Sharcnet, specifically on the Orca development nodes.

I have started this article as opposed to just forwarding to the Oracle page so that I will be able to post answers to common questions here, or linked to here. On that note, feel free to update this page with your experiences/tips, and forward any bugs, questions, or problems to Ed Armstrong (edward@sharcnet.ca), or submit a ticket in the SHARCNET problem tracking system.

The following is the Oracle license agreement, which applies to the code samples in this article.

Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
  - Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  - Neither the name of Oracle or the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Thread Creation, Execution, and Control

There are two ways to create and start a Java thread. The first involves implementing the Runnable[2] interface and passing an instance to the Thread class constructor. The second involves extending the Thread[3] class it's self, and overriding the provided, yet empty, run method.

public class HelloRunnable implements Runnable {
    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }
}
public class HelloThread extends Thread {
    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new HelloThread()).start();
    }
}

Thread.sleep()