PCB : Process 정보 저장
fork() system call에 의해 생성main() {
int pid;
cout << "just one process so far" << endl;
pid = fork();
if (pid == 0)
cout << "i'm the child" << endl;
else if (pid > 0)
cout << "i'm the parent" << endl;
else
cout << "fork failed" << endl;
}

address 주소의 최대값은
class MyThread extends Thread {
public void run() {
// ...work to do
}
}
MyThread t = new MyThread();
t.start();
위와 같이 thread를 실행하려면 run()을 실행

class ThreadDemo {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
for (int i = 0; i < 50; i++) {
System.out.println("i = " + i + ", i * i = " + i * i);
}
}
}
class MyThread extends Thread {
public void run() {
for (int count = 1, row = 1; row < 20; row++, count++) {
for (int i = 0; i < count; i++) {
System.out.print('*');
}
System.out.print('\n');
}
}
}
위 코드에서 run() 먼저 실행
Thread myThread = new Thread("HappyThread");
public class Loop3 extends Thread {
public Loop3(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(getName() + " (" + i + ")");
try {
sleep(10);
}
catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
Loop3 t1 = new Loop3("Thread 1");
Loop3 t2 = new Loop3("Thread 2");
Loop3 t3 = new Loop3("Thread 3");
t1.start();
t2.start();
t3.start();
}
}
Java는 다중 상속을 허용하지 않음

Runnable interface는 하나의 method를 가지고 있음class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable.run()");
}
}
class Main {
public static void main(String[] args) {
MyRunnable myrun = new MyRunnable();
Thread t1 = new Thread(myrun);
t1.start();
System.out.println("InsideMain()");
}
}
class YieldThread extends Thread {
public void run() {
for (int count = 0; count < 4; count++) {
System.out.println(count + "From: " + getName());
yield();
}
}
}
class TestThread {
public static void main(String[] args) {
YieldThread first = new YieldThread();
YieldThread second = new YieldThread();
first.start();
second.start();
System.out.println("End");
}
}
//thread A 안
threadB.join() // B가 종료될 때까지 A는 sleeppublic class JoinThr {
static public void main(String s[]) {
MyThread1 Thread_a;
MyThread2 Thread_b;
Thread_a = new MyThread1();
Thread_b = new MyThread2(Thread_a);
System.out.println("Starting the threads...");
Thread_a.start();
Thread_b.start();
}
}
class MyThread1 extends Thread {
public void run() {
System.out.println(getName() + " is running...");
for (int i = 0; i < 4; i++) {
try {
sleep(500);
}
catch (InterruptedException e) {}
System.out.println("Hello there, from" + getName());
}
}
}
class MyThread2 extends Thread {
private Thread wait4me;
MyThread2(Thread target) {
super();
wait4me = target;
}
public void run() {
System.out.println(getName() + " is waiting for " + wait4me.getName() + "...");
try {
wait4me.join();
}
catch (InterruptedException e) {}
System.out.println(wait4me.getName() + " has finished...");
for (int i = 0; i < 4; i++) {
try {
sleep(500);
}
catch (InterruptedException e) {}
System.out.println("Hello there, from" + getName());
}
}
}
output
Starting the threads...
Thread-0 is running...
Thread-1 is waiting for Thread-0...
Hello there, fromThread-0
Hello there, fromThread-0
Hello there, fromThread-0
Hello there, fromThread-0
Thread-0 has finished...
Hello there, fromThread-1
Hello there, fromThread-1
Hello there, fromThread-1
Hello there, fromThread-1
ex. Bank Account
class Account {
int balance;
public void deposit(int val) {
balance = balance + val;
}
}
thread가 account balance에 동시에 deposit을 하면 race condition이 됨
synchronized keyword를 사용해 controll accesss 가능class Account {
private int balance;
public synchronized void deposit(int val) {
balance = balance + val;
}
public synchronized void withdraw(int val) {
balance = balance - val;
}
}