Jump to content

Recommended Posts

Posted

Boa tarde, estou a tentar elaborar um  programa em linguagem JAVA que lance a execução de 5 threads. Cada thread

deve escrever 200 numeros num ficheiro de texto. Tendo que garantir a exclusão

mútua nas operações de escrita. O nome do ficheiro devera ser passado

como argumento da linha de comandos.

O problema é que não sei como fazer para saber qual o thread que imprimiu no ficheiro, ou seja, no ficheiro deveria aparcer th1 - 0 até 199, th2 200-399 e assim sucessivamente...

Eis o meu código:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package socp_guiao_4;

import java.util.concurrent.Semaphore;

import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.*;

public class exercicio_1 implements Runnable {

    Semaphore s;
    int n;

    public exercicio_1(Semaphore sem, int i) {
        s = sem;
        n = i;
    }

    public void run() {
        String myname = Thread.currentThread().getName();
        System.out.println("[" + myname + "] Inicio da thread");
        try {
            Thread.sleep(n * 1000);
        } catch (InterruptedException iex) {
        }
        s.release();
        System.out.println("[" + myname + "] Fim da thread");
    }

    /**
     *
     * @author JAVA_PROG
     */
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        Charset ENCODING = StandardCharsets.UTF_8;
        Path path = Paths.get("linhas.txt");
        String linha = "Exemplo de Linha";
        List<String> linhas = new ArrayList<>();

        for (int i =0; i<1000; i++) {

            linhas.add("" + i + ":" + linha);
            Files.write(path, linhas, ENCODING);
        }

        Semaphore sem = new Semaphore(5);
        for (int i = 0; i < 5; i++) {
            try {
                sem.acquire();
            } catch (InterruptedException iex) {
            }
            Thread th = new Thread(new Current(sem, i), "Th" + i);
            th.start();
        }
    }
}
Posted

Podes passar um id diferente para cada thread, por exemplo, receber esse parâmetro no construtor da thread.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.