Class BinarySemaphore

Similar documents
Concurrent Programming using Threads

Unit - IV Multi-Threading

7. MULTITHREDED PROGRAMMING

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Threads Chate Patanothai

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

public static boolean isoutside(int min, int max, int value)

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Object Oriented Programming (II-Year CSE II-Sem-R09)

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Multithreaded Programming

Threads in Java (Deitel & Deitel)

Multithread Computing

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

public static void negate2(list<integer> t)

Java Threads. Written by John Bell for CS 342, Spring 2018

Java Threads. Introduction to Java Threads

CMSC 132: Object-Oriented Programming II. Threads in Java

Overview of Java Threads (Part 2)

Threads Questions Important Questions

Multi-threaded programming in Java

EDAF65 Processes and Threads

What is a thread anyway?

EDA095 Processes and Threads

Object Oriented Programming. Week 10 Part 1 Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Chapter 19 Multithreading

CS 351 Design of Large Programs Threads and Concurrency

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

CS 556 Distributed Systems

Amity School of Engineering

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Unit III Rupali Sherekar 2017

Performance Throughput Utilization of system resources

Techniques of Java Programming: Concurrent Programming in Java

Model Requirements and JAVA Programs MVP 2 1

User Space Multithreading. Computer Science, University of Warwick

Introduction to Java Threads

Figure 6.1 System layers

Module - 4 Multi-Threaded Programming

Java Threads. COMP 585 Noteset #2 1

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Contents. 6-1 Copyright (c) N. Afshartous

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

EDA095 Processes and Threads

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Program Submission Guidelines CIS/CMSC 274

Java s Implementation of Concurrency, and how to use it in our applications.

CS1 Studio Project: Connect Four

Multi-threading in Java. Jeff HUANG

Programming Language Concepts: Lecture 11

JThreads/C++ Version 2.0.0b1. IONA Technologies PLC

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Chapter 11 Paper Practice

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

CPS 506 Comparative Programming Languages. Programming Language Paradigms

Robotics and Autonomous Systems

Concurrent Programming

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

CIS233J Java Programming II. Threads

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Time-Sharing Operating Systems. EDA095 Processes and Threads. Process Creation. The Content of a Process

Chapter 13. Concurrency ISBN

ATAPI Java Reference Documentation

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

The class Object. Lecture CS1122 Summer 2008

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

THREADS AND CONCURRENCY

Parallel Programming Practice

CS360 Lecture 10 Multithreading

The UCSC Java Nanokernel Version 0.2 API

G52CON: Concepts of Concurrency

Parallel Programming Practice

Software Practice 1 - Multithreading

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

COMP 401 ANIMATION, THREADS, COMMAND OBJECTS. Instructor: Prasun Dewan

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

15CS45 : OBJECT ORIENTED CONCEPTS

Definition: A thread is a single sequential flow of control within a program.

Program #3 - Airport Simulation

Only one thread can own a specific monitor

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

Global shared variables. Message passing paradigm. Communication Ports. Port characteristics. Sending a message 07/11/2018

public class Shared0 { private static int x = 0, y = 0;

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Object-Oriented Programming Concepts-15CS45

Concurrent, Real-Time and Distributed Programming in Java

Transcription:

Java Kernel API

Class BinarySemaphore java.lang.object +--BinarySemaphore public final class BinarySemaphore extends java.lang.object This class represents a binary semaphore. The value of a binary semaphore can be either 0 or 1. Constructor Summary BinarySemaphore(int value) Constructs a binary semaphore. Method Summary void binsignal() Increments the semaphore if it is 0. void binwait() Decrements the semaphore if it is 1. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Constructor Detail BinarySemaphore public BinarySemaphore(int value) Constructs a binary semaphore. 1

2 Class BinarySemaphore value - The initial value of the semaphore (0 or 1). Method Detail binwait public void binwait() Decrements the semaphore if it is 1. Blocks until binsignal() is called if it is 0. binsignal public void binsignal() Increments the semaphore if it is 0. Undefined if it is 1.

Class EventSet java.lang.object +--EventSet public class EventSet extends java.lang.object This class represents a set of events. Constructor Summary EventSet() Constructs an empty set of events. EventSet(EventSet eventset) Constructs a set of events containing the same events as another set. EventSet(int e1) Constructs a set of one event. EventSet(int e1, int e2) EventSet(int e1, int e2, int e3) EventSet(int e1, int e2, int e3, int e4) EventSet(int e1, int e2, int e3, int e4, int e5) Method Summary EventSet copy() Returns a copy of this set. void copy(eventset eventset) Overwrites (copies into) this set with another set, destroying this set. void copy(int e) Removes all events from the set, except for one event, which is added. boolean empty() Checks to see of the set is empty. boolean equals(eventset ceventset) 3

4 Class EventSet Checks to see if two sets are equal. void excl(eventset eventset) Removes a set of event from the this set. void excl(int e) boolean has(int e) Checks if an event is included in the set. void incl(eventset eventset) Adds a set of events to this set. void incl(int e) Adds an event to this set. void intersection(eventset eventset) Makes this set of ervent the intersection of this set and the supplied set. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Constructor Detail EventSet public EventSet() Constructs an empty set of events. EventSet public EventSet(int e1) Constructs a set of one event. e1 - The event to be a member of the set. EventSet public EventSet(int e1, int e2)

Class EventSet 5 EventSet public EventSet(int e1, int e2, int e3) EventSet public EventSet(int e1, int e2, int e3, int e4) EventSet public EventSet(int e1, int e2, int e3, int e4, int e5) EventSet public EventSet(EventSet eventset) Constructs a set of events containing the same events as another set. eventset - The set of events to be contained in this set. Method Detail incl public void incl(eventset eventset) Adds a set of events to this set. eventset - The set of events to add.

6 Class EventSet incl public void incl(int e) Adds an event to this set. i - the ervent to add excl public void excl(eventset eventset) Removes a set of event from the this set. eventset - The event to remove. excl public void excl(int e) copy public EventSet copy() Returns a copy of this set. Returns: The copy. copy public void copy(eventset eventset) Overwrites (copies into) this set with another set, destroying this set. eventset - The set to overwrite this set with.

Class EventSet 7 copy public void copy(int e) Removes all events from the set, except for one event, which is added. i - The event to add. intersection public void intersection(eventset eventset) Makes this set of ervent the intersection of this set and the supplied set. eventset - The set to take intersection with. has public boolean has(int e) Checks if an event is included in the set. e - The event to check. Returns: true if the event is in the set; false otherwise. empty public boolean empty() Checks to see of the set is empty. Returns: true if the set is empty. equals public boolean equals(eventset ceventset) Checks to see if two sets are equal. ceventset - The set to check with.

Class GeneralSemaphore Class GeneralSemaphore java.lang.object +--GeneralSemaphore public final class GeneralSemaphore extends java.lang.object This class represents a general semaphore. The value of a general semaphore can be greater than or equal to 0. Constructor Summary GeneralSemaphore(int i) Constructs a general semaphore. Method Summary void gensignal() Increments the semaphore. void genwait() Decrements the semaphore if it is larger than 0. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Constructor Detail GeneralSemaphore public GeneralSemaphore(int i) Constructs a general semaphore. 8

Class GeneralSemaphore 9 i - The initial value of the semaphore ( 0). Method Detail genwait public void genwait() Decrements the semaphore if it is larger than 0. Blocks until binsignal() is called if it is 0. gensignal public void gensignal() Increments the semaphore.

Class Kernel java.lang.object +--java.lang.thread +--Kernel All Implemented Interfaces: java.lang.runnable public abstract class Kernel extends java.lang.thread This class is an extension to java that supports multiple processes and message passing synchronization as well as event handling. The kernel has two modes of operation: init-mode and normal-mode. In init-mode, the kernel operates exactly as a normal java program. Init mode is intended for process creation and general setup. Processes are created with the createstdprocess() method call. After the setup and process creation is finished, the kernel can be switched to normal-mode with the startprocesses() method call. The call to startprocesses() will never return, but all processes created with createstdprocess() will start to execute concurrently. This is a simple example that shows basic process creation: public class Exempel1 { public static void main(string[] args) { //create objects representing the process Kernel process1 = new TestProcess("Process 1", "Hello Anna!"); Kernel process2 = new TestProcess("Process 2", "Hello Bertil!"); } } //start processes (leave init-mode) Kernel.startProcesses(); public class TestProcess extends Kernel { private String message; TestProcess(String name, String message) { 10

Class Kernel 11 //tell the kernel that this is a process createstdprocess(name, NoTimer); } this.message = message; } //this method represents the process public void run() { System.out.println(message); } Field Summary protected static int Alarm This is used as argument to createstdprocess and means that the process will receive a timeevent after i milliseconds, where i is the argument of settimebase(). protected static int ev0 protected static int ev1 protected static int ev10 protected static int ev11 protected static int ev12 protected static int ev13 protected static int ev2 protected static int ev3 protected static int ev4 protected static int ev5

12 Class Kernel protected static int ev6 protected static int ev7 protected static int ev8 protected static int ev9 protected static int MonoStable This is used as argument to createstdprocess and means that select() or await() will wait at most i milliseconds before they will receive a timeevent, where i is the argument of settimebase() protected java.lang.object NoData A dummy message. protected static int NoTimer This is used as argument to createstdprocess and means that no timer will be used for this process. protected static int Periodic This is used as argument to createstdprocess and means that a timeevent will be sent to this process every i:th millisecond, where i is the argument of settimebase(). protected static int TimeEvent This is the event that is received after invoking settimebase(). Fields inherited from class java.lang.thread MAX PRIORITY, MIN PRIORITY, NORM PRIORITY Constructor Summary Kernel() Method Summary protected java.lang.object asyncreceive() Receives an asynchronous message. protected void asyncsend(java.lang.object message) Sends an asynchronous message to this process. protected void await(int event) Waits for an event.

Class Kernel 13 protected Kernel createstdprocess(java.lang.string name, int timermode) Creates a process and sets the timermode of the process. protected void nap(long ms) Makes the process block for a specified amount of time. protected Message receive() Extracts a pending message sent by protected void reply(java.lang.object message) Sends the reply message to a process that has invoked sendwait() on this process. protected int select(eventset eventset) Waits for a set of events. protected java.lang.object sendwait(int event, java.lang.object message) Implements extended rendezvous. protected void settimebase(long ms) Sets the timebase for this process. protected void signal(int event) Sends an event to this process. void start() static void startprocesses() Starts all defined processes. protected java.lang.object syncreceive() Receives a synchronous message. protected void syncsend(java.lang.object cmessage) Sends a synchronous message to this process. Methods inherited from class java.lang.thread activecount, checkaccess, countstackframes, currentthread, destroy, dump- Stack, enumerate, getcontextclassloader, getname, getpriority, getthread- Group, interrupt, interrupted, isalive, isdaemon, isinterrupted, join, join, join, resume, run, setcontextclassloader, setdaemon, setname, setpriority, sleep, sleep, stop, stop, suspend, tostring, yield Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, wait, wait, wait Field Detail TimeEvent protected static final int TimeEvent This is the event that is received after invoking settimebase().

14 Class Kernel ev0 protected static final int ev0 ev1 protected static final int ev1 ev2 protected static final int ev2 ev3 protected static final int ev3 ev4 protected static final int ev4 ev5 protected static final int ev5

Class Kernel 15 ev6 protected static final int ev6 ev7 protected static final int ev7 ev8 protected static final int ev8 ev9 protected static final int ev9 ev10 protected static final int ev10 ev11 protected static final int ev11

16 Class Kernel ev12 protected static final int ev12 ev13 protected static final int ev13 NoTimer protected static final int NoTimer This is used as argument to createstdprocess and means that no timer will be used for this process. This means that a call to settimebase() will be an error. Periodic protected static final int Periodic This is used as argument to createstdprocess and means that a timeevent will be sent to this process every i:th millisecond, where i is the argument of settimebase(). MonoStable protected static final int MonoStable This is used as argument to createstdprocess and means that select() or await() will wait at most i milliseconds before they will receive a timeevent, where i is the argument of settimebase() Alarm protected static final int Alarm This is used as argument to createstdprocess and means that the process will receive a timeevent after i milliseconds, where i is the argument of settimebase().

Class Kernel 17 NoData protected final java.lang.object NoData A dummy message. Use when no message is needed. Constructor Detail Kernel public Kernel() Method Detail start public final void start() Overrides: start in class java.lang.thread startprocesses public static final void startprocesses() Starts all defined processes. Processes are defined through createstd- Process(). settimebase protected final void settimebase(long ms) Sets the timebase for this process. After invoking this method, a timeevent will be generated according to the timermode of the process. The timermode for the process is defined through createstdprocess. ms - The timebase for this process, in milliseconds.

18 Class Kernel nap protected final void nap(long ms) Makes the process block for a specified amount of time. This requires the process to have timer, which means that you cannot supply NoTimer to createstdprocess(). ms - Milliseconds to block. createstdprocess protected Kernel createstdprocess(java.lang.string name, int timermode) Creates a process and sets the timermode of the process. Returns: name - The name of the process. timermode - The timermode of the process. Can be NoTimer, Periodic, Monostable or Alarm. A reference to the created process (same as the object reference to the object representing the process). sendwait protected final java.lang.object sendwait(int event, java.lang.object message) Implements extended rendezvous. A process can send a message to this process by invoking this method on this process. This process will then receive an event and a message. The event should be received by await() or select() and the message should be received by receive(). This process should then reply to the sender, who is blocked, with reply() and the sender will then receive the message supplied to reply(). event - The event that this process should receive. message - The message that this process should receive.

Class Kernel 19 receive protected final Message receive() Extracts a pending message sent by Returns: An instance of Message that contains the message received and the sender of the message (a reference). signal protected final void signal(int event) Sends an event to this process. This should be invoked by a process that wants this process to receive an event. event - The event to be sent. await protected final void await(int event) Waits for an event. The event can be generated by signal(), sendwait(), or can be a TimeEvent. This method will block until the event is received. event - The event to wait for. reply protected final void reply(java.lang.object message) Sends the reply message to a process that has invoked sendwait() on this process. This will release that process. message - The reply message.

20 Class Kernel select protected final int select(eventset eventset) Waits for a set of events. This method will block until an event included in the set is received. Returns: ceventset - The set of events to wait for. The event received. syncsend protected final void syncsend(java.lang.object cmessage) Sends a synchronous message to this process. A process invoking this method will block until this process (the recipient) invokes syncreceive(). cmessage - The message to send. syncreceive protected final java.lang.object syncreceive() Receives a synchronous message. Send() is invoked on this process. Returns: The message received. This method will block until sync- asyncsend protected final void asyncsend(java.lang.object message) Sends an asynchronous message to this process. A process invoking this will not block. message - The message to send.

Class Kernel 21 asyncreceive protected final java.lang.object asyncreceive() Receives an asynchronous message. This method will block until async- Send() is invoked on this process. Returns: The message received.

Class MailBox java.lang.object +--MailBox public class MailBox extends java.lang.object This class is implements a mailbox, also known as asynchronous channel. Constructor Summary MailBox() Constructs an empty mailbox. Method Summary java.lang.object check() Retrives a mail from the mailbox. void mail(java.lang.object cmessage) Posts an object to the mailbox. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Constructor Detail MailBox public MailBox() Constructs an empty mailbox. Method Detail 22

Class MailBox 23 mail public void mail(java.lang.object cmessage) Posts an object to the mailbox. object - The object to post to the mailbox. check public java.lang.object check() Retrives a mail from the mailbox. This method will block if the mailbox is empty until there is an object in the mailbox.

Class MChannel java.lang.object +--MChannel public class MChannel extends java.lang.object This class represents a synchronous many-to-many channel. Constructor Summary MChannel() Constructs a synchronous many-to-many channel. Method Summary java.lang.object read() Reads an object from the channel (blocks until write() is invoked). void write(java.lang.object object) Writes an object into the channel and waits for a receiver. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Constructor Detail MChannel public MChannel() Constructs a synchronous many-to-many channel. Method Detail 24

Class MChannel 25 write public void write(java.lang.object object) Writes an object into the channel and waits for a receiver. object - The object to put in the channel. read public java.lang.object read() Reads an object from the channel (blocks until write() is invoked). Returns: The object supplied to write().

Class Message java.lang.object +--Message public class Message extends java.lang.object This class represents a message received by the Kernel.receive() method in Kernel. Method Summary java.lang.object getmessage() Returns the message. Kernel getprocess() Returns the process reference. Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, wait, wait Method Detail getprocess public Kernel getprocess() Returns the process reference. Returns: The sending process. When the Messsage object is returned by Kernel.receive(), this is a reference to the sending process. 26

Class Message 27 getmessage public java.lang.object getmessage() Returns the message. Returns: The message sent. When returned by Kernel.receive(), this is the message sent (passed to Kernel.sendWait() of the sending process).