Académique Documents
Professionnel Documents
Culture Documents
IOException, ClassNotFoundException {
int ctr=0;
Scanner s1=new Scanner(new File(fileName));
while(s1.hasNextLine()) {
ctr=ctr+1;
s1.next();
}
stream=new int[ctr];
extends Thread
implements Runnable acelasi lucru.
while true. si in main
@Override
public void run() {
try {
for (int i=0; i<5; i++) {
System.out.println(this.toString());
Thread.sleep(this.timpDeLucru);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t1.start();
t2.start();
runn
Runnable r1 = new DeparteAproape("aproape", 50);
Runnable r2 = new DeparteAproape("DEPARTE", 100);
// new Thread(r1).start();
// new Thread(r2).start();
t1.start();
t2.start();
package multithreading_2;
return -1;
}
}
package multithreading_2;
@Override
public void run() {
while (true) {
try {
Thread.sleep(this.timpOperare);
operareCont(suma);
} catch (InterruptedException e) {
// Nu fac nimic, ci doar returnez
return;
}
}
}
cont.setOperare(true);
System.out.println("Soldul inainte de
operare " +
numeOperator + ": " +
cont.getSold());
// cont.depunere(suma);
if (cont.retragere(suma) < 0) {
System.out.println("Intrerupere
pentru suma: " +
suma);
eliberareCont();
Thread.currentThread().interrupt();
}
eliberareCont();
}
} catch (InterruptedException e) {
// System.out.println("Intrerupere pentru suma: "
+
// suma);
// eliberareCont();
}
}
cont.setOperare(false);
notifyAll();
}
}
operatorA.start();
operatorB.start();
operatorC.start();
try {
operatorA.join();
operatorB.join();
operatorC.join();
// Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// operatorA.interrupt();
// operatorB.interrupt();
// operatorC.interrupt();
import java.util.concurrent.Semaphore;
return -1;
}
package multithreading_3;
@Override
public void run() {
try {
while (true) {
Thread.sleep(timpOperare);
// synchronized(this) {
operareCont(suma);
// }
}
} catch (InterruptedException e) {
// Nu am nimic de facut
return;
}
}
System.out.println("Sold inainte de
operare " +
this.numeOperator + ": " +
this.cont.getSold());
if (cont.retragere(suma) < 0) {
System.out.println("Operator " +
this.numeOperator + " a fost
intrerupt");
eliberareCont();
Thread.currentThread().interrupt();
}
eliberareCont();
}
}
}
cont.ReleaseOperare();
// }
}
operatorA.setName("Thread A");
operatorB.setName("Thread B");
operatorC.setName("Thread C");
operatorC.setPriority(MAX_PRIORITY);
operatorA.start();
operatorB.start();
operatorC.start();
Thread[] threadArray = new
Thread[Thread.activeCount()];
Thread.enumerate(threadArray);
for (int i=0; i<threadArray.length; i++)
System.out.println(threadArray[i].getName() +
" cu prioritatea " +
threadArray[i].getPriority());
try {
// sleep(3000);
operatorA.join();
operatorB.join();
operatorC.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader reader=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(reader);
System.out.println("a=");
try {
String input=br.readLine();
a=Float.parseFloat(input);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("b=");
try {
String input=br.readLine();
b=Float.parseFloat(input);
} catch (IOException e) {
e.printStackTrace();
}
catch(NumberFormatException e) {
System.out.println("Number is not in proper
format");
e.printStackTrace();
}
System.out.println("a +b =" +(a+b));
FileOutputStream fos=null;
OutputStreamWriter writer=null;
try {
fos=new FileOutputStream("robot.txt");
writer=new OutputStreamWriter(fos);
writer.write(robot.getName()+ "");
writer.write(System.lineSeparator());
writer.write(String.format("%.2f",
robot.getAutonomy()));
writer.write(System.lineSeparator());
writer.write(String.format("%d",
robot.getProductionYear()));
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(writer !=null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos !=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try(FileInputStream fis=new
FileInputStream("robot.txt")){
try(InputStreamReader is=new
InputStreamReader(fis)){
try(BufferedReader buffer=new
BufferedReader(is)){
System.out.println(buffer.readLine());
System.out.println(buffer.readLine());
System.out.println(buffer.readLine());
}
}
}
catch(IOException e ) {
e.printStackTrace();
}