surya.n.lokhande
BAN USER//Sample code -
class PrintWord {
boolean m_state;
void printHello() {
System.out.print("Hello");
}
void printWorld() {
System.out.println("World");
}
void setState(boolean state) {
m_state = state;
}
boolean getState() {
return m_state;
}
}
class HelloThread extends Thread {
PrintWord printWord;
HelloThread(PrintWord value) {
printWord = value;
}
public void run() {
while (true) {
synchronized (printWord) {
if (!printWord.getState()) {
printWord.printHello();
printWord.setState(true);
printWord.notify();
} else {
try {
printWord.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class WorldThread extends Thread {
PrintWord printWord;
WorldThread(PrintWord value) {
printWord = value;
}
public void run() {
while (true) {
synchronized (printWord) {
if (printWord.getState()) {
printWord.printWorld();
printWord.setState(false);
printWord.notify();
} else {
try {
printWord.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class HelloWorld {
public static void main(String[] args) {
PrintWord printWord = new PrintWord();
HelloThread helloThread = new HelloThread(printWord);
WorldThread worldThread = new WorldThread(printWord);
helloThread.start();
worldThread.start();
}
}
import java.util.ArrayList;
- surya.n.lokhande October 27, 2013import java.util.List;
public class ArrayBytes {
public static void main(String[] args) {
List<String> arrayOfBytes = new ArrayList<String>(5);
// sample binary number starting from 10 to 15
// will be initially stored in array-
for (int j = 10; j < 15; j++) {
arrayOfBytes.add(Integer.toBinaryString(j));
}
long count = 0;
for (String string : arrayOfBytes) {
System.out.println(string);
char[] charArray = string.toCharArray();
for (char c : charArray) {
if (c == '1') {
count++;
}
}
}
// total count of 1 in all the elements of the array printed here
System.out.println(count);
}
}