1. When your program starts, it shall do the following: 1. Create a file, SHARED
ID: 3557500 • Letter: 1
Question
1. When your program starts, it shall do the following:
1. Create a file, SHARED.txt, in the current directory (cwd).
2. Write it's pid (Process ID) followed by a Carriage Return and Newline in the file.
3. Close the file SHARED.txt
4. Create a semaphore named SEM which the threads will use to manage access to the file
SHARED.txt.
5. Create 6 threads. Use the POSIX version of threads (i.e., pthread_create())
6. Block/wait for all six threads to complete their work.
7. Destroy the semaphore, then exit gracefully, printing a friendly message to the console
2. Each thread shall perform the following (note, each thread is running concurrently):
1. Periodically (even numbered threads - once every two seconds, odd numbered threads
Explanation / Answer
java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author
*/
public class ThreadLocalTest {
public static void main(String args[]) throws IOException {
Thread t1 = new Thread(new Task());
Thread t2 = new Thread( new Task());
t1.start();
t2.start();
}
/*
* Thread safe format method because every thread will use its own DateFormat
*/
public static String threadSafeFormat(Date date){
DateFormat formatter = PerThreadFormatter.getDateFormatter();
return formatter.format(date);
}
}
/*
* Thread Safe implementation of SimpleDateFormat
* Each Thread will get its own instance of SimpleDateFormat which will not be shared between other threads. *
*/
class PerThreadFormatter {
private static final ThreadLocal<SimpleDateFormat> dateFormatHolder = new ThreadLocal<SimpleDateFormat>() {
/*
* initialValue() is called
*/
@Override
protected SimpleDateFormat initialValue() {
System.out.println("Creating SimpleDateFormat for Thread : " + Thread.currentThread().getName());
return new SimpleDateFormat("dd/MM/yyyy");
}
};
/*
* Every time there is a call for DateFormat, ThreadLocal will return calling
* Thread's copy of SimpleDateFormat
*/
public static DateFormat getDateFormatter() {
return dateFormatHolder.get();
}
}
class Task implements Runnable{
@Override
public void run() {
for(int i=0; i<2; i++){
System.out.println("Thread: " + Thread.currentThread().getName() + " Formatted Date: " + ThreadLocalTest.threadSafeFormat(new Date()) );
}
}
}
Output:
Creating SimpleDateFormat for Thread : Thread-0
Creating SimpleDateFormat for Thread : Thread-1
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Read more: http://javarevisited.blogspot.com/2012/05/how-to-use-threadlocal-in-java-benefits.html#ixzz32z6b3QGY
java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author
*/
public class ThreadLocalTest {
public static void main(String args[]) throws IOException {
Thread t1 = new Thread(new Task());
Thread t2 = new Thread( new Task());
t1.start();
t2.start();
}
/*
* Thread safe format method because every thread will use its own DateFormat
*/
public static String threadSafeFormat(Date date){
DateFormat formatter = PerThreadFormatter.getDateFormatter();
return formatter.format(date);
}
}
/*
* Thread Safe implementation of SimpleDateFormat
* Each Thread will get its own instance of SimpleDateFormat which will not be shared between other threads. *
*/
class PerThreadFormatter {
private static final ThreadLocal<SimpleDateFormat> dateFormatHolder = new ThreadLocal<SimpleDateFormat>() {
/*
* initialValue() is called
*/
@Override
protected SimpleDateFormat initialValue() {
System.out.println("Creating SimpleDateFormat for Thread : " + Thread.currentThread().getName());
return new SimpleDateFormat("dd/MM/yyyy");
}
};
/*
* Every time there is a call for DateFormat, ThreadLocal will return calling
* Thread's copy of SimpleDateFormat
*/
public static DateFormat getDateFormatter() {
return dateFormatHolder.get();
}
}
class Task implements Runnable{
@Override
public void run() {
for(int i=0; i<2; i++){
System.out.println("Thread: " + Thread.currentThread().getName() + " Formatted Date: " + ThreadLocalTest.threadSafeFormat(new Date()) );
}
}
}
Output:
Creating SimpleDateFormat for Thread : Thread-0
Creating SimpleDateFormat for Thread : Thread-1
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.