Threads

Question:
I recently took Sun’s Certified Java Programmers Exam and I ran into a few questions from the exam I’d like to ask you.

What is the difference between >> and >>>? For example let’s say you have:

int x = 64;     what is x >> 4?     what is x >>> 4?
I get 4 for both.

What can be said about garbage collection? Select all true statements:

a. It will be done so that your program will never run out of memory (within reason).
b.It happens at predictable times.
c.You can flag an object for collection.
d.You can call a method to have an object explicitly collected.

Under which circumstance would a thread stop execution? Select all true statements:

a.A thread with a higher priority becomes run-able.
b.The priority of the running thread is changed.
c.Thread objects of the same class terminates.

If you have an object and then you set it to null:

SomeObject ob = new SomeObject();      ob = null;
after you set ob to null, is the object available for garbage collection?

Answer:
Thanks for your questions. It’s interesting for us to see the typesof questions on Sun’s own exam.

First question: 64 >> 4 = 64 >>> 4 = 4

Explanation:

 x >> y is arithmetic shift right of x by y bits   x >>> y is logical shift right of x by y bits
The difference is logical: Right shift uses 0 as a fill bit,while arithmetic shift right uses the most significant bitof x as the fill bit. Thus, logical shift right correspondsto unsigned integer division by two, while arithmetic shiftright corresponds to signed integer division by two.

Since 64 > 0, the answer is the same, but in the eight-bit 2’scomplement representation, -64 = 11000000. Hence-64 >> 4 = 11111100 = -4, while -64 >>> 4 = 00001111 = 15.

What about left shift? Fortunately, the same fill bit, 0,is used for unsigned and signed multiplication by two. Hencex << y corresponds to x * 2^y no matter what the sign ofx is.

Garbage Collection

a. I’m going to answer true to a. It would be hard to writea program that gobbled up the entire heap without releasingany of it. Reading a giant file into a hash table coulddo the trick.

b. No. The Java garbage collector runs in a low priority thread. Insome systems the thread is scheduled when System.gc() isexplicitly called by a program, or when the heap runs low.On other systems gc() runs whenever the system is idle, and isinterrupted whenever a higher priority thread becomes run-able.

c. An object is implicitly flagged for collection as soon asthere are no more active references to the object. Explicitlyflagging an object for collection would imply there was anactive reference to the object ? otherwise, how would one explicitlyflag it? ? hence, when the object is collected, we are left witha dangling reference.

d.Programs can directly call the garbage collector:

System.gc();
But asking for a specific object to be collected implies onehas an active reference to the object, which would leavea dangling reference after collection.

Thread scheduling

a. The Java thread scheduler is preemptive in the sense that itwill preempt a running thread if a higher priority thread becomesrun-able. But don’t depend on this; the Java scheduler may runa lower priority thread to prevent starvation.

b. Yes. I tried the following simple experiment:

class MyThread extends Thread {   int id;   MyThread(int i) { id = i; }   public void run() {      for(int j = 0; j < 3; j++) {         System.out.println("Thread " + id + " running");         setPriority(1);  // 5 = normal      }   }}public class Test {   public static void main(String[] args) {      MyThread t1 = new MyThread(1);      MyThread t2 = new MyThread(2);      t1.start();      t2.start();   }}
On SunOS 5.5.1, I saw:
Thread 1 running   Thread 2 running   Thread 2 running   Thread 2 running   Thread 1 running   Thread 1 running
(I guess they didn’t let you use computers during the test. Too bad!)

c. My example shows the answer to c is no. Thread 2 stoppedrunning, but that didn’t stop thread 1. Of course threads inthe same ThreadGroup can be terminated simultaneously.

Breaking an active reference to an object:

ob = null;
doesn’t imply the object becomes garbage unless this was the lastactive reference to the object.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a