forked from 0xqab/concepts-of-programming-languages
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var myYoutubevideo struct { | ||
likes int32 | ||
} | ||
|
||
func Viewer() { | ||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) | ||
atomic.AddInt32(&myYoutubevideo.likes, 1) | ||
} | ||
|
||
func main() { | ||
wg := sync.WaitGroup{} | ||
for i := 0; i < 10000; i++ { | ||
wg.Add(1) | ||
go func() { | ||
Viewer() | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
fmt.Println(myYoutubevideo.likes, "likes") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
java threads.java | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
int numberOfTasks = 1000; | ||
int threadPoolSize = 1000; | ||
|
||
ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize); | ||
|
||
for (int i = 0; i < numberOfTasks; i++) { | ||
executorService.submit(() -> { | ||
try { | ||
Thread.sleep(10000); // Wait for 3 seconds | ||
System.out.println("Thread " + Thread.currentThread().getId() + " finished waiting."); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
|
||
executorService.shutdown(); | ||
|
||
try { | ||
// Optional: Wait for all tasks to finish execution | ||
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- - Implement a converter function of an integer to a binary string. | ||
-- | ||
-- ```Haskell | ||
-- intToBinary :: Int -> String | ||
-- .... | ||
-- | ||
-- main = print $ intTooBinary 42 -- returns "101010" | ||
-- ``` | ||
|
||
intToBinary :: Int -> String | ||
intToBinary 0 = "0" | ||
intToBinary n = reverse (binary n) | ||
where | ||
binary 0 = "" | ||
binary n = show (n `mod` 2) ++ binary (n `div` 2) | ||
|
||
main = do | ||
print $ intToBinary 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
conn, err := net.Dial("tcp", "localhost:8081") | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
reader := bufio.NewReader(os.Stdin) | ||
for { | ||
input, _, err := reader.ReadRune() | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = conn.Write([]byte(string(input))) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
}() | ||
for { | ||
_, err := io.Copy(os.Stdout, conn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |