How to download all pieces of a Torrent

I am trying to implement a BitTorrent client and have implemented many features including parsing the file and connecting to peers and read/send info from the socket but now I am confused on how I put all this together.

public void send_handshake() throws IOException {

        byte[] l = {(byte) 19}, name = "BitTorrent protocol".getBytes(), reserved_bytes = new byte[8],
                info_hash = torrent.info_hash_as_binary, peer_id = "12345678900987654321".getBytes();

        byte[] c = combine(l,name,reserved_bytes,info_hash,peer_id);
        out.write(c);

        System.out.println("Sent Handshake");

    }
public void send_request() throws Exception {

       //variables initialized

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

            bytes_offset = ByteBuffer.allocate(4).putInt(i*(int)Math.pow(2,14)).array();
            block_length = ByteBuffer.allocate(4).putInt((int)Math.pow(2,14)).array();

            if(lastblocklen!=0 && i==numofblocks-1){
                block_length = ByteBuffer.allocate(4).putInt((int)lastblocklen).array();
                System.out.println(Main.ANSI_BLUE + "Last block length is  :  " + new BigInteger(block_length).intValueExact() + " its size : "+ block_length.length + Main.ANSI_RESET);
            }

            out.write(SendRequest); // Sendrequest = all the byte array fields combined

            System.out.println("Thread : "+ thread_num +" : " + "////// Sent request Message for block " + i + " of piece " + next_piece +" //////");

         }
    }
 public int read_message() throws Exception {

        boolean handshake = false;
        byte[] len = new byte[4];

        int offset=0;
        while(offset<len.length) {
            int t = in.read(len, offset, len.length - offset);
            if(t>0)offset += t;
            if(t==-1) return -1; //EOS error
        }
        if((int)len[0]==19 && (byte)len[1]=='B') handshake = true;

        int length_of_message = new BigInteger(len).intValue();

        byte[] messageid = new byte[1];
        int rep = in.read(messageid,0,1);
        int message_id = new BigInteger(messageid).intValueExact();

        if(handshake){
            // READ HANDSHAKE MESSAGE
        }
        else if (message_id == 5) {
            try {
                read_Bitfield(length_of_message);
                return 0;
            }
            catch (Exception e){
                throw new Exception();
            }
        }
        else if (message_id == 4) {
                read_Have(length_of_message);

        }
        else if (message_id == 1) {
            //UNCHOKE MESSAGE

            return 1; //unchoke

        }
        else if(message_id == 0){
            //CHOKE MESSAGE
            return 3;
        }
        else if(message_id == 7){
            read_piece(length_of_message);
        }
        else if(message_id == 8){
            //READ CANCEL MESSAGE
        }
        else {
            System.out.println("message id not matched - message id : " + message_id);
            throw new Exception();
        }
        return 10;
    }
public int read_piece(int length_of_message) throws Exception {
        //PIECE MESSAGE
        //variables initialized
        while(off<index_of_piece.length) {
            int t1 = in.read(index_of_piece, off, index_of_piece.length - off);
            if (t1 > 0) off += t1;
        }
        off = 0;
        while(off<bytes_sent.length) {
            int t1 = in.read(bytes_sent, off, bytes_sent.length - off);
            if (t1 > 0) off += t1;
        }
        off = 0;
        while(off<data.length) {
            int t1 = in.read(data, off, data.length - off);
            if (t1 > 0) off += t1;
        }
        int index = new BigInteger(index_of_piece).intValueExact();


        File f = new File(path + index); //path = location to my program directory

        try (FileOutputStream fos = new FileOutputStream(path+index, true)) {
            fos.write(data);
        }
        int bytes_sent_integer = new BigInteger(bytes_sent).intValueExact();
        double blocknum = (bytes_sent_integer/Math.pow(2,14));

        System.out.println("Thread : " + thread_num + " Written (Appended) block " + blocknum + " to piece file " + index);

        if(blocknum==numofblocks-1){
            System.out.println("Thread : " + thread_num + " ////// We now have piece "+ index + " //////");
            if(torrent.piece_hash_values_as_hex.contains(
                    Utils.bytesToHex(createSha1(new File(path+index))).toUpperCase())
            ) {
                System.out.println("Thread : "+ thread_num +" : " + "CORRECTLY DOWNLOADED PIECE " + index);
                synchronized (pieces){
                    pieces[index] = 1;
                }
            }
            else{
                System.out.println("Hash Mismatch...");

                synchronized (pieces){
                    System.err.println("Piece value of " + index + " set to "+0);
                    pieces[index] = 0;
                }
                return -7;
            }
        }
        return 0;
    }

I tried making two threads, one that sends request packets to a connected peer and one that continuously reads the InputStream from the socket for responses but it fails due to improper synchronization between the threads (at the end of the download the reader thread reading wrong values continuously). Please suggest possible ways I can implement this.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật